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.
|
which language, how do you want to display (gui maybe?). Can you please give more details
|
This is the C++ code you can check:
Code:
#include <iostream.h> |
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? |
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] |
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: you can organize for loops based on these examples: http://www.cyberciti.biz/faq/bash-for-loop/ Code:
for (( i=1; i<=$n1; i++ )) Code:
printf "%s\n" "${array[@]}" |
All times are GMT -5. The time now is 06:15 PM. |