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: John Kloss
Subject: Re: [Help-bash] Printing from Array
Date: Thu, 21 May 2015 13:07:15 -0400

Use the prefix increment operator _not_ the postfix operator

for myColumn in $MyData; do
    MyArray[++q]="${myColumn#*}"
    ...

Output:

1:
1: One
2: One
2: Two
3: Two
3: Three

I think I might have pulled the increment operation out of the array
index lookup so that the logic would have been clearer.

for myColumn in $MyData; do
    (( q++ ))
    MyArray[q]="${myColumn#*}"
    ...

Just my $.2

  John.

On Thu, May 21, 2015 at 12:52 PM, Richard Taubo <address@hidden> wrote:
> Hei!
>
> Creating a simple array, I am just wondering why the second printf line
> does not print any results from the array at all.
> I was assuming it would print the "two" and "three" lines in the array.
>
> Thanks for feedback!
>
> #!/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
>
> Best regards,
> Richard Taubo
>



reply via email to

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