0

I was reading the post here and tried out the solutions.

However, the following does not produce any output. What am I missing?

arr=( '102 351' '576 324' '427 321' )
for str in "$arr[@]";                                                                                                                                                                                  ✔ 
do ./findgcd $str | cat >> output;
done;

output file remains empty but no error is seen.

Giacomo1968
  • 58,727
kesarling
  • 209
  • 2
  • 11

1 Answers1

0

Unlike some other shells, zsh does not automatically split variables on whitespace, so the findgcd call is being passed a single string. The showArgs function in the script below demonstrates what happens and how you can force word-spliting with the $= expansion:

#!/usr/bin/env zsh

showArgs() { printf -- '%-9s:' "${1}" shift printf ' "%s"' "$@" print }

arr=( '102 351' '576 324' '427 321' ) for str in "$arr[@]"; do showArgs 'No split' ${str} showArgs 'Split' ${=str} # ./findgcd ${=str} >> output findgcd ${=str} print done

Output:

No split : "102 351"
Split    : "102" "351"
3

No split : "576 324" Split : "576" "324" 36

No split : "427 321" Split : "427" "321" 1


Another way to process an array as pairs in zsh:

arr=(
  102 351
  576 324
  427 321
)
for a b in $arr; do
    showArgs 'pairs' $a $b
    findgcd $a $b
done

Output:

pairs    : "102" "351"
3
pairs    : "576" "324"
36
pairs    : "427" "321"
1
Gairfowl
  • 556