So I can do arithmetics on the amount of apples and bananas. Which is the missing command?
\nAnother way to put the question: How do I run a command for every column of a row?
","upvoteCount":3,"answerCount":2,"acceptedAnswer":{"@type":"Answer","text":"When you parse the string you end up getting two columns of strings. One way would be to convert the columns to integer. I always use the command describe to understand what I'm working with.
\nThis will show you that they are parsed as strings
\necho \"12apples 25bananas\" | parse \"{apples}apples {bananas}bananas\" | describe
Then you can convert the columns into integer. You could run describe on this to see the outcome
\necho \"12apples 25bananas\" | parse \"{apples}apples {bananas}bananas\" | into int apples bananas
Then to run on each row you use the command each. $it indicates item/iterator meaning row here.
\necho \"12apples 25bananas\" | parse \"{apples}apples {bananas}bananas\" | into int apples bananas | each { echo $\"you have (($it.apples) + ($it.bananas)) fruit\"}
-
|
I'm trying to do something like this: So I can do arithmetics on the amount of apples and bananas. Which is the missing command? Another way to put the question: How do I run a command for every column of a row? |
Beta Was this translation helpful? Give feedback.
-
|
When you parse the string you end up getting two columns of strings. One way would be to convert the columns to integer. I always use the command describe to understand what I'm working with. This will show you that they are parsed as strings Then you can convert the columns into integer. You could run describe on this to see the outcome Then to run on each row you use the command |
Beta Was this translation helpful? Give feedback.
-
|
It may seem odd to reply to this 3 years after it was answered, but since it's a top Google result and the current answer is not directly pertinent to the question's title, I'm providing my own variant. The most apt choice would be the $ ["test -d /mnt/D/DCIM", "test -r /mnt/D/DCIM"] | split column ' '
╭───┬─────────┬─────────┬─────────────╮
│ # │ column1 │ column2 │ column3 │
├───┼─────────┼─────────┼─────────────┤
│ 0 │ test │ -d │ /mnt/D/DCIM │
│ 1 │ test │ -r │ /mnt/D/DCIM │
╰───┴─────────┴─────────┴─────────────╯
$ ["test -d /mnt/D/DCIM", "test -r /mnt/D/DCIM"] | split column ' ' | each {values}
╭───┬─────────────────────╮
│ 0 │ ╭───┬─────────────╮ │
│ │ │ 0 │ test │ │
│ │ │ 1 │ -d │ │
│ │ │ 2 │ /mnt/D/DCIM │ │
│ │ ╰───┴─────────────╯ │
│ 1 │ ╭───┬─────────────╮ │
│ │ │ 0 │ test │ │
│ │ │ 1 │ -r │ │
│ │ │ 2 │ /mnt/D/DCIM │ │
│ │ ╰───┴─────────────╯ │
╰───┴─────────────────────╯Still new to this language, so if there's a technically superior method of achieving this result that is better - I am unaware. |
Beta Was this translation helpful? Give feedback.
When you parse the string you end up getting two columns of strings. One way would be to convert the columns to integer. I always use the command describe to understand what I'm working with.
This will show you that they are parsed as strings
echo "12apples 25bananas" | parse "{apples}apples {bananas}bananas" | describeThen you can convert the columns into integer. You could run describe on this to see the outcome
echo "12apples 25bananas" | parse "{apples}apples {bananas}bananas" | into int apples bananasThen to run on each row you use the command
each.$itindicates item/iterator meaning row here.echo "12apples 25bananas" | parse "{apples}apples {bananas}bananas" | into int apples ba…