help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Printing from Array


From: Dennis Williamson
Subject: Re: [Help-bash] Printing from Array
Date: Thu, 21 May 2015 12:28:40 -0500

Perhaps you want ++q instead of q++, but note that will skip MyArray[0]
(meaning it will be null/unset).

On Thu, May 21, 2015 at 12:02 PM, Greg Wooledge <address@hidden> wrote:

> On Thu, May 21, 2015 at 06:52:08PM +0200, Richard Taubo wrote:
> > Creating a simple array, I am just wondering why the second printf line
> > does not print any results from the array at all.
>
> > #!/bin/bash
> > MyData="One,two,three"
> > declare -i q
> > OIFS=$IFS
> > IFS=$','
> > for myColumn in $MyData; do
> >         MyArray[q++]="${myColumn#*}"
> >         printf "%s\n" "$q: ${MyArray[q-1]}"
> >         printf "%s\n" "$q: ${MyArray[q]}"
> > done
> > IFS=$OIFS
>
> After the first MyArray[q++] line, you have an array with one element
> (element 0) and q is set to 1.  So when you try to reference MyArray[1]
> there's nothing there.
>
> The same thing occurs each time through the loop.
>
>


-- 
Visit serverfault.com to get your system administration questions answered.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]