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: Chris F.A. Johnson
Subject: Re: [Help-bash] Printing from Array
Date: Thu, 21 May 2015 23:26:27 -0400 (EDT)
User-agent: Alpine 2.10 (DEB 1266 2009-07-14)

On Thu, 21 May 2015, Richard Taubo 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

   Your problem has already been explained, but I would suggest doing
   it differently (assuming that the info is already in a
   comma-delimited variable):

IFS=, read -ra MyArray <<< "$MyData" ## read variable into an array

for (( q = 1; q < address@hidden; q++))
do
  printf "%s\n" "$((q-1)): ${MyArray[q-1]}"
  printf "%s\n" "$q: ${MyArray[q]}"
done

   Or:

for q in "address@hidden"
do
  ((q==0))&&continue
  printf "%s\n" "$((q-1)): ${MyArray[q-1]}"
  printf "%s\n" "$q: ${MyArray[q]}"
done
for q in "address@hidden"
do
  ((q==0))&&continue
  printf "%s\n" "$((q-1)): ${MyArray[q-1]}"
  printf "%s\n" "$q: ${MyArray[q]}"
done

--
Chris F.A. Johnson, <http://cfajohnson.com>



reply via email to

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