Help how to make awk array

Hi expert,
I'm newbie in awk programming and now I have difficulty how to make awk array in ash shell. I have code below.
As you know ash shell doesn't support array and I cannot install bash
in my embedded environment.
So in order this code to work in ash shell then all array needs to be converted
Problem now while creating array in ash shell :

   scene[$SCNCNT]=$(echo $line | awk '{ print $2 }')

and calling array in ash shell :

   echo "MSG~heyu $(echo ${scene[${phone#*~}]})" | tee -a $LOG
   $HEYU $(echo ${scene[${phone#*~}]})

Can somebody help me on this?

#!/bin/sh

HEYU=/usr/bin/heyu	      # Path to Heyu
SED=/usr/bin/sed
SORTAZ=0                      # If 0, will sort by house code.

get_scenes() {
   SCENES=$($HEYU show usersyns | grep "usersyn")
   if [ "$SORTAZ" -eq "1" ]
    then 
       SCENES=$(echo "$SCENES" | sort)
   fi
   IFS=$(echo -en "\n\b")
   for line in $SCENES
    do
       let SCNCNT=$SCNCNT+1
       scene[$SCNCNT]=$(echo $line | awk '{ print $2 }')
       echo -n "S: " >> $LOG
       echo $line | awk '{ print "SCENE~" $2 "~'$SCNCNT'" }' | sed 's/_/ /g' | \
       sed 's/.*/\L&/; s/[a-z]*/\u&/g' | tee -a $LOG
   done
   IFS=$ORIGIFS
}

set_scene() {
   echo -n "S: " >> $LOG
   echo "MSG~heyu $(echo ${scene[${phone#*~}]})" | tee -a $LOG
   $HEYU $(echo ${scene[${phone#*~}]})
   sleep 1
   get_updates "$DEVICES"
} 

get_scenes
set_scene

The awk array is very simple, just start using it and it is created. It is really a hash map. Your challenge is to learn enough awk to put the array to use. There are many fine online tutorials, just Google 'awk tutorial array', like: AWK Arrays Explained with 5 Practical Examples

I suspect you will have more problems than just the arrays. For example the sed uses GNU extensions, which are likely not supported by the (busybox?) version in your embedded environment, nor is the -e option to echo. Also the code seems inefficient with several calls to subshells and external commands within a loop.

Could you provide a sample output of

/usr/bin/heyu | grep "usersyn"
1 Like

yes, i need to install gnu sed instead of using busybox sed
gnu sed doesn't need to have lots of memory space, so i'm ok
but bash needs lots of memory, my embedded device can't have this

btw i have resolve the issue above by replacing

scene[$SCNCNT]=$(echo $line | awk '{ print $2 }')

to

eval ${scene}${SCNCNT}=$(echo $line | awk '{ print $2 }')
echo "MSG~heyu $(echo ${scene[${phone#*~}]})" | tee -a $LOG

to

echo "MSG~heyu $(echo $(eval echo \$$scene${phone#*~}))" | tee -a $LOG
$HEYU $(echo ${scene[${phone#*~}]})

to

$HEYU $(echo $(eval echo \$$scene${phone#*~}))

After replace, everything works as expected
However now I have another problem in my other code using array variable
The code is below
How to modify this code to work with ash shell ??

LIST=(1 2 3)
for i in "${LIST[@]}"; do
  echo "example.$i "
done

Try:

LIST="1 2 3"
for i in $LIST; do
  echo "example.$i "
done
1 Like

thank's, it works
i have another array like this below
i can replace

$HEYU preset ${TH}${sparray[$TEMP]}

to

$HEYU preset ${TH}$(eval echo \$$sparray$TEMP)

however how to change this constant array?

sparray=([20]="1 17" [21]="1 18" [22]="1 19" [23]="1 20" [24]="1 21" \
               [25]="1 22" [26]="1 23" \
               [68]="3 1" [69]="3 2" [70]="3 3" [71]="3 4" [72]="3 5" \
               [73]="3 6" [74]="3 7" [75]="3 8" [76]="3 9" [77]="3 10" \
               [78]="3 11" [79]="3 12")

TEMP=$(echo ${phone} | awk -F'~' '{ print $4 }')
$HEYU preset ${TH}${sparray[$TEMP]}

Use linefeed as row sep and 'while read' to unpack:

LIST='1 a A
2 b B
3 c B'
 
echo "$LIST"|while read l
do
 echo "l=$l"
done
 
echo "$LIST"|while read x y z
do
 echo "x=$x y=$y z=$z"
done

If you have just one column with embedded whitespace, make it the last column so 'read ... z' scoops it up.

1 Like

this works well
thanks