This sounds easy, but is much more difficult than you think using a fully POSIX compliant shell.
This is fully POSIX compliant and gets 39 individual words into individual variables just as you wanted.
Arrays per-se do not exist in POSIX but this creates a pseudo-array.
With a random file of words these variables have to be searched and the value used as the new variable with a relevant integer assigned to it.
Lots of conditional tasks as well as variable searching.
The rest should be easy.
#!/bin/sh
# #!/usr/local/bin/dash
# Tested using dash as the shell.
# Generate a file containing required words.
echo 'one two
buckle my shoe
three four
knock at the door
five six
pick up sticks
seven eight
lay them straight
nine zero
a big fat hero
multiply add
and maybe to subtract
divide too
just to satisfy you' > /tmp/txt
# Convert basic text file to single word lines.
tr ' ' '\n' < /tmp/txt > /tmp/text
# *******************************************
# **** The main pseudo-array generator. *****
INDEX=1
while read -r ARRAY
do
eval MY_ARRAY${INDEX}='${ARRAY}'
INDEX=$(( INDEX+1 ))
done < /tmp/text
# You now have a number of variables containing
# 1 word in each. The above is POSIX compliant.
# *******************************************
# Check the environment to see if the variables are there.
set
And the results, viewing the environment variables.
thanks alot, i thought about comparing every word with its number using case or if but then i was stuck in making that one two three a full integer 123, but with the array it works now, thanks alot wisecracker and rudic.
#!/bin/bash
# bash-4 required
declare -A sym
sym=(
[zero]=0
[one]=1
[two]=2
[three]=3
[four]=4
[five]=5
[plus]=+
)
while read -r -a words
do
out=""
for word in "${words[@]}"
do
out+=${sym[$word]-" $word "}
done
echo "$out"
done
it worked, thanks alot. How can i get better at this, like i didn't think of $((out)), i thought of using "if" and conditions to compare it with the input that i give which will make my code even more complicated. Any tips for that ?. Thanks alot brother
What can I say? Read, exercise, be open. This forum (amongst others) is great for learning. Look around, find all types of problems, scrutinize and understand the proposed solutions. Apply what you learned. Solve found problems on your own, and compare to what others proposed. man pages are your friends.