0

this the code segment

declare -A m;
 
for(( i=1;i<=n;i++ ))
do
  for (( j=1;j<=n;j++ ))
  do
    m[i,j]=0;
  done
done

Is there any option to reduce this code in a single line ? or is any improvement possible ?

  • `Is there any option to reduce this code in a single line ?` `declare -A m; for(( i=1;i<=n;i++ )); do for (( j=1;j<=n;j++ )); do m[i,j]=0; done; done` . You do know, that your code only ever assigns _one_ element with index `"i,j"`? No expansion happens there, it''s a stirng `i,j`. – KamilCuk Jun 14 '22 at 13:00
  • bash does not have 2D-arrays, so you have to simulate it somehow, for instance using namerefs. – user1934428 Jun 14 '22 at 13:48
  • `eval "tmps=('m['{1..$n}','{1..$n}']=0')"; eval "${tmps[@]}"` works, but many would not consider it to be an improvement. – pjh Jun 14 '22 at 13:49

0 Answers0