PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > Arrays in bash
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Arrays in bash

Réponse
 
LinkBack Outils de la discussion
Vieux 10/12/2006, 15h16   #1
Antonio Maschio
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Arrays in bash

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
  Réponse avec citation
Vieux 10/12/2006, 23h19   #2
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Arrays in bash

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
  Réponse avec citation
Vieux 12/12/2006, 18h52   #3
Dave Gibson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Arrays in bash

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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 22h28.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11336 seconds with 11 queries