|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hi,
I have this little script that sets up two lines in BASIC (or whatever data made by a couple number/text), and put the data in an array element whose index is the number and whose argument is the text: -- BEGIN script -- # set first line, intentionally the second ln=10 pr="PRINT I" prog[$ln]="$pr" # set second line, which is the first ln=5 pr="REM PROVA" prog[$ln]="$pr" # list lines in order - print only the text, for now! for i in "${prog[@]}"; do printf "%s\n" "$i" done -- END script -- How can I access the inner index (here 5 and 10) to print along the $i content and change the printf to printf "%d %s" <what?> "$i" ? Thanks -- Antonio |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On 2006-12-10, Antonio Maschio wrote:
> Hi, > > I have this little script that sets up two lines in BASIC (or whatever > data made by a couple number/text), and put the data in an array element > whose index is the number and whose argument is the text: > > -- BEGIN script -- > > # set first line, intentionally the second > ln=10 > pr="PRINT I" > prog[$ln]="$pr" > > # set second line, which is the first > ln=5 > pr="REM PROVA" > prog[$ln]="$pr" > > # list lines in order - print only the text, for now! > for i in "${prog[@]}"; do > printf "%s\n" "$i" > done The loop is unnecessary if all you want to do is print the members of the array: printf "%s\n" "${prog[@]}" > -- END script -- > > How can I access the inner index (here 5 and 10) to print along the $i > content and change the printf to > > printf "%d %s" <what?> "$i" > > ? n=0 i=0 while [ "$n" -lt "${#prog[@]}" ] do if [ -n "${prog[$i]}" ] then printf "%d %s\n" "$i" "${prog[$i]}" n=$(( $n + 1 )) fi i=$(( $i + 1 )) done -- Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell> Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress) ===== My code in this post, if any, assumes the POSIX locale ===== and is released under the GNU General Public Licence |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
Chris F.A. Johnson <cfajohnson@gmail.com> wrote:
> On 2006-12-10, Antonio Maschio wrote: >> Hi, >> >> I have this little script that sets up two lines in BASIC (or whatever >> data made by a couple number/text), and put the data in an array element >> whose index is the number and whose argument is the text: [ prog[5]="REM PROVA" ; prog[10]="PRINT I" for i in "${prog[@]}" ; do printf '%s\n' "$i" ; done ] >> How can I access the inner index (here 5 and 10) to print along the $i >> content and change the printf to >> >> printf "%d %s" <what?> "$i" >> >> ? > > n=0 > i=0 > while [ "$n" -lt "${#prog[@]}" ] > do > if [ -n "${prog[$i]}" ] if [ "${prog[$i]+x}" = "x" ] # to handle empty elements (prog[7]="") > then > printf "%d %s\n" "$i" "${prog[$i]}" > n=$(( $n + 1 )) > fi > i=$(( $i + 1 )) > done To the OP: Bash (as of version 3.0) and ksh93 can list array indices: for i in "${!prog[@]}" ; do printf '%d %s\n' "$i" "${prog[$i]}" done |
|
![]() |
| Outils de la discussion | |
|
|