LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-17-2008, 04:33 PM   #1
akitty
LQ Newbie
 
Registered: Nov 2008
Posts: 9

Rep: Reputation: 0
how to implement bidimensional arrays in awk?


hello everyone!!! a have some really big question marks and i hope u can give me some answers!
lets say i have an input file like this :
a b d g
a b c d e f g h
a c d f g h
a b c d g

i need to arrange the fields in an output file like this:
a b - d - - g -
a b c d e f g h
a - c d - f g h
a b c d - - g -

for this i came up with the script below :


awk ; { if(max_nf<NF)
max_nf=NF;
max_nr=NR
for(i=1;i<=NF;i++)
v[i,NR]=$i;
}
END {for (i=1;i<=max_nr;i++)
for(j=1;j<=max_nf;j++)
{if (j==1 && v[j,i]!="a")
{for(k=max_nf;k>j;k--)
v[k,i]=v[k-1,i];
v[j,i]="-";}
------------------------
if (j==8 && v[j,i]!="a")
{for(k=max_nf;k>j;k--)
v[k,i]=v[k-1,i];
v[j,i]="-";}
}
for(i=1;i<=max_nr;i++)
{for(j=1;j<=max_nf;j++)
printf("%s ",v[j,i]);
printf("\n");}
}


Now, if i run the script on a Ubuntu system it works perfectly!
But i really HAVE to run it on a Solaris 5.9 and there is the problem!!!
Apparently i has "syntax errors" at assigning v[i,Nr]=$i, and at all "for" loops .
So, it's not a code problem, but a syntax one.
The thing is both systems have /usr/bin/awk and /usr/bin/nawk.
How can i find out what my version of awk can support and what it can't?
And more importantly, how do i rewrite the script in order to work on the Solaris?
Hope anyone cand help me
 
Old 11-17-2008, 06:08 PM   #2
yowi
Member
 
Registered: Dec 2002
Location: Au
Distribution: Debian
Posts: 209

Rep: Reputation: 52
Solaris has old school versions of such utilities.
Linux has the latest, greatest GNU versions.
On my system awk is symlinked to mawk for example.

If your script won't run under nawk then try gawk.
You can get gawk for solaris from blastwave.org

The awk lineage goes something like:
awk
n(ew)awk
g(nu)awk
m(?)awk
what's next, squawk???

WP has some history, if you're interested:
http://en.wikipedia.org/wiki/Awk

Last edited by yowi; 11-17-2008 at 06:12 PM.
 
Old 11-18-2008, 04:25 AM   #3
burschik
Member
 
Registered: Jul 2008
Posts: 159

Rep: Reputation: 31
GNU awk does not have multi-dimensional arrays. It simulates multi-dimensional arrays by concatenating the indices and using the result as an index for a one-dimensional array. You can easily do this yourself by writing

Code:
array["1" "," "2"]
instead of

Code:
array[1, 2]
 
Old 11-18-2008, 07:54 AM   #4
akitty
LQ Newbie
 
Registered: Nov 2008
Posts: 9

Original Poster
Rep: Reputation: 0
thank u both for your suggestion...
i don't have permission to update the system , so i can't get gawk.
i will try instead simulating the multi dimensional array.
i'' be back to tell u if it worked.
thanks again!
 
  


Reply

Tags
funny


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
shell command using awk fields inside awk one71 Programming 6 06-26-2008 05:11 PM
Some comments on awk and awk scripts makyo Programming 4 03-02-2008 06:39 PM
Arrays of Structures to Arrays of Classes knobby67 Programming 1 01-01-2008 02:39 PM
Passing bidimensional arrays to function in C pittopitto Programming 5 05-18-2006 10:31 AM
Question about outputing arrays with pointers, then just arrays... RHLinuxGUY Programming 1 04-12-2006 06:40 AM


All times are GMT -5. The time now is 05:00 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration