LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 04-12-2012, 02:57 AM   #1
kakalig2007
LQ Newbie
 
Registered: Jul 2008
Posts: 11

Rep: Reputation: 0
2d array


I know that this is silly but I cannot find anything on the web on how to read numbers from keyboard for a 2d array in bash shell script and to display it.

Last edited by kakalig2007; 04-13-2012 at 01:55 AM. Reason: 2d array in bash shell script
 
Old 04-12-2012, 03:30 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,349

Rep: Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748
which language, how do you want to display (gui maybe?). Can you please give more details
 
Old 04-12-2012, 02:56 PM   #3
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,415

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
This is the C++ code you can check:

Code:
#include <iostream.h>
#include <iomanip.h>
#include <fstream.h>
#include <conio.h>
using namespace std;

const int ROWS = 2;
const int COLUMNS = 3;

int main() {
  ifstream inFile;
  int myArray[ROWS][COLUMNS];  
  int r;
  int c;

  inFile.open("test.txt");
  if (!inFile) {
    cout << "Unable to open file";
    exit(1);
  }

  for(r=0; r<ROWS; r++) {
    for(c=0; c<COLUMNS; c++) {
      inFile >> myArray[r][c];
    }
  }

    for(r=0; r<ROWS; r++) {
    for(c=0; c<COLUMNS; c++) {
      cout << myArray[r][c] << " ";
    }
    cout << endl;
  }
  inFile.close();
  return 0;
}
Hope it will help you.

Last edited by Satyaveer Arya; 04-12-2012 at 02:58 PM.
 
Old 04-13-2012, 11:50 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
bash only has simple one-dimensional arrays. You have to use a more advanced language, like awk or perl, to get into higher dimensions.

Check out the array section of the gawk manpage, for example.
http://www.gnu.org/software/gawk/man...de/Arrays.html


(Edit: And see here for arrays in bash: http://mywiki.wooledge.org/BashFAQ/005)


How about explaining in detail exactly what you want to do, and give a real example of the input you have and the output you want?

Last edited by David the H.; 04-13-2012 at 11:53 AM. Reason: as stated
 
Old 04-16-2012, 02:48 AM   #5
kakalig2007
LQ Newbie
 
Registered: Jul 2008
Posts: 11

Original Poster
Rep: Reputation: 0
I would like input as in c/c++:
for (i=0;i<n1;i++)
for (j=0;j<n2;j++)
read a[i][j] /*a[i][j] is the array name */

/* do some processing*/

and for output:
for (i=0;i<n1;i++)
for (j=0;j<n2;j++)
write a[i][j]
 
Old 04-16-2012, 03:38 AM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,349

Rep: Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748
there is no 2d array in bash, so you need to simulate it somehow. if you need only numbers (as you wrote in your first post) you can have a chance, but in general there will be no solution. Also bash is limited in size, it will not be able to handle huge arrays.

So first you need to read dimensions, it is something like this:
Code:
echo -n enter n1:
read n1
echo -n enter n2:
read n2
you need some check on these inputs, but in this simple case we will skip this step.
you can organize for loops based on these examples: http://www.cyberciti.biz/faq/bash-for-loop/
Code:
for (( i=1; i<=$n1; i++ ))
do
  for (( j=1; j<=$n2; j++ ))
  do
    echo -n enter element'['$i']['$j']':
    read a
    array[$i]+=" $a"
  done
done
finally you can print this 'pseudo-2d' array:
Code:
printf "%s\n" "${array[@]}"
 
  


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] mdadm: only give one device per ARRAY line: /dev/md/:raid and array laughing_man77 Linux - Hardware 4 03-23-2012 04:05 PM
Server array (mysql, php) -> gboolean array in c client kalleanka Programming 1 07-27-2010 06:50 AM
[SOLVED] shell script help: copying directory list into an array and then accessing the array richman1234 Linux - Newbie 6 07-25-2010 11:19 PM
[bash] indirect array reference to array with values containing spaces Meson Linux - Software 9 06-04-2010 09:38 PM
bash: use file as input into array, parse out other variables from array using awk beeblequix Linux - General 2 11-20-2009 10:07 AM


All times are GMT -5. The time now is 02:50 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