LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-06-2011, 12:34 PM   #1
odstderek
LQ Newbie
 
Registered: Nov 2011
Location: USA
Distribution: Backtrack
Posts: 9

Rep: Reputation: Disabled
Printing Columns of Arrays


I'm trying to print the contents of 2 arrays in columns side-by-side, with some space in between them. This is what I have:
Code:
for i in {1..$elsecount..1}
do
  echo -ne ${original[$i]}
  echo -ne "         "
  echo ${correction[$i]}
done
Unfortunately this is causing 2 problems. First, the loop doesn't execute properly, I get this error (line 41 is the line of this loop):
Code:
./Z1596828.spell: line 41: {1..2..1}: syntax error: operand expected (error token is "{1..2..1}")
And when I just try to echo these 2 arrays with a variable as the index, I get blank lines. If I try printing them like this
Code:
echo -ne ${original[*]}
I can't prevent it from printing the entire array on one line, so I can't make it 2 columns.

Any ideas on getting this loop to work and getting these arrays to print properly? Thanks

(This is in a BASH script, on a Ubuntu system)
 
Old 11-06-2011, 12:46 PM   #2
odstderek
LQ Newbie
 
Registered: Nov 2011
Location: USA
Distribution: Backtrack
Posts: 9

Original Poster
Rep: Reputation: Disabled
I just changed it to a while loop with a counter, which I hate doing cause that's exactly what for loops are designed for, but oh well. I just need to know how to print an array with a variable as the index.
 
Old 11-06-2011, 01:01 PM   #3
odstderek
LQ Newbie
 
Registered: Nov 2011
Location: USA
Distribution: Backtrack
Posts: 9

Original Poster
Rep: Reputation: Disabled
And I got it to work. For anyone who sees this later and is curious:
Code:
let counter=1
echo -e "\nMISSPELLED:\tCORRECTIONS:\n"
while [ $counter -lt $elsecount ]
do
  echo -e "${original[$counter]}\t\t${correction[$counter]}"
  let counter=$counter+1
done
 
Old 11-06-2011, 01:17 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
The increment in the extended brace expansion is a feature introduced in Bash version 4.0. To run the script with a older version of bash you should use seq instead. Or if the increment is 1 (as in your example) you can use the brace expansion without specifying it. This one should work:
Code:
for i in $(seq 1 1 $elsecount)
do
  echo -e "${original[$i]}\t\t${correction[$i]}"
done
Also note that arrays in bash are indexed starting from 0, whereas you are printing from 1 (the second element of the array).
 
Old 11-06-2011, 02:39 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
Slightly simpler:

Code:
for i in "${!original[@]}"; do

	echo -e "${original[i]}\t\t${correction[i]}"

done
Assuming that the indexes match up in both arrays, and that you want to print the whole thing, of course.


Edit: BTW, don't forget that there's also the c-style for loop.

Code:
for (( i=0 ; i < ${#original[@]} ; i++ )); do

	echo -e "${original[i]}\t\t${correction[i]}"

done

Last edited by David the H.; 11-06-2011 at 02:44 PM. Reason: as stated
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] AWK: add columns while keep format for other columns cristalp Programming 3 10-13-2011 07:14 AM
printing 2 arrays to 2 columns. casperdaghost Programming 2 06-16-2010 08:27 AM
printing multiple columns with awk kdelover Programming 16 12-16-2009 10:10 AM
Bash - How can you list 2 arrays in columns zak111 Programming 12 07-23-2007 12:27 PM
printing eqivalenced arrays using gdb, g77, Linux tdr Programming 0 02-09-2004 04:39 AM


All times are GMT -5. The time now is 09:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration