Computing.Net > Forums > Unix > convert column to associative array

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

convert column to associative array

Reply to Message Icon

Original Message
Name: ragu
Date: April 29, 2005 at 14:08:11 Pacific
Subject: convert column to associative array
OS: solaris 8
CPU/Ram: intel
Comment:

hi
Can any of the awk gurus around show me how to read 2 columns from a file and load them into an associative array. I have to do this from a ksh script.
e.g if i have a file with 2 columns

Stock Price
---------------------
IBM 21
INTEL 12

I should be able to get the price using a map(associative array)stock_price like:
price = stock_price[IBM]

thnx in advance
Ragu



Report Offensive Message For Removal


Response Number 1
Name: nails
Date: April 29, 2005 at 16:05:16 Pacific
Reply: (edit)


echo ""|nawk ' {
while (getline < "mydata" > 0)
stock_price[$1]=$2

printf("%d\n", stock_price["IBM"])
printf("%d\n", stock_price["INTEL"])

} '



Report Offensive Follow Up For Removal

Response Number 2
Name: ragu
Date: May 2, 2005 at 08:41:02 Pacific
Reply: (edit)

Works like a charm !
Thnx a ton Nails, really appreciate it.


Report Offensive Follow Up For Removal







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home








Do you have your own blog?

Yes
No
I did before
I will soon


View Results

Poll Finishes In 2 Days.
Discuss in The Lounge
Poll History




Data Recovery Software