Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Want a clean way to populate associative array
Hello, fellow gawkers ...

I'm looking for a "clean" and legible way to populate a 50-element
associative array in GNU awk. I like the syntax of Perl or PHP

"apple"    => "red",
"banana"   => "yellow",
"orange"   => "orange",

But I know that it's not native to awk. I suggested a very legible
method of populating an array of month-names and days-per-month not so
long ago, but that case was a short example that easily fit on one
line. This case now is a much longer situation where the keys are 30
to 50 characters in length.

Does anyone know of a pre-written awk function or an inventive way for
populating hashes or associative arrays that is quite legible? I know
that I could write

arr["apple"]    = "red"
arr["banana"]   = "yellow"
arr["orange"]   = "orange"

but I was also toying with the idea of generating a tab-separated list
of key-value pairs and having awk slurp up the list and parse it. Any
ideas?

--
Eric Pement

Report this thread to moderator Post Follow-up to this message
Old Post
Eric Pement
12-01-04 08:55 PM


Re: Want a clean way to populate associative array
Eric Pement <pemente@northpark.edu> wrote:
> Hello, fellow gawkers ...
>
> I'm looking for a "clean" and legible way to populate a 50-element
> associative array in GNU awk. I like the syntax of Perl or PHP
>
>    "apple"    => "red",
>    "banana"   => "yellow",
>    "orange"   => "orange",
>
> But I know that it's not native to awk. I suggested a very legible
> method of populating an array of month-names and days-per-month not so
> long ago, but that case was a short example that easily fit on one
> line. This case now is a much longer situation where the keys are 30
> to 50 characters in length.
>
> Does anyone know of a pre-written awk function or an inventive way for
> populating hashes or associative arrays that is quite legible? I know
> that I could write
>
>    arr["apple"]    = "red"
>    arr["banana"]   = "yellow"
>    arr["orange"]   = "orange"
>
> but I was also toying with the idea of generating a tab-separated list
> of key-value pairs and having awk slurp up the list and parse it.

Just do that.

> Any ideas?



--
William Park <opengeometry@yahoo.ca>
Linux solution for data processing.

Report this thread to moderator Post Follow-up to this message
Old Post
William Park
12-01-04 08:55 PM


Re: Want a clean way to populate associative array
Eric Pement wrote:
>
> Does anyone know of a pre-written awk function or an inventive way for
> populating hashes or associative arrays that is quite legible? I know
> that I could write
>
>    arr["apple"]    = "red"
>    arr["banana"]   = "yellow"
>    arr["orange"]   = "orange"
>
> but I was also toying with the idea of generating a tab-separated list
> of key-value pairs and having awk slurp up the list and parse it. Any
> ideas?

BEGIN { FS = "\t"
while (getline <"yourfile" >0)
arr[$1] = $2
}


Janis

Report this thread to moderator Post Follow-up to this message
Old Post
Janis Papanagnou
12-02-04 01:55 AM


Re: Want a clean way to populate associative array
In article <227a55e9.0412011052.68a1bc22@posting.google.com>,
Eric Pement <pemente@northpark.edu> wrote:
>Does anyone know of a pre-written awk function or an inventive way for
>populating hashes or associative arrays that is quite legible? I know
>that I could write
>
>   arr["apple"]    = "red"
>   arr["banana"]   = "yellow"
>   arr["orange"]   = "orange"
>
>but I was also toying with the idea of generating a tab-separated list
>of key-value pairs and having awk slurp up the list and parse it. Any
>ideas?

There's a function 'Assign' in ftp://ftp.armory.com/pub/lib/awk/array
that does this.  It may be overkill for your application...

John
--
John DuBois  spcecdt@armory.com  KC6QKZ/AE  http://www.armory.com/~spcecdt/

Report this thread to moderator Post Follow-up to this message
Old Post
John DuBois
12-02-04 01:55 AM


Re: Want a clean way to populate associative array
BEGIN {
s = \
"apple,red,banana,yellow,orange,orange"\
",averylongnameforadeliciousfruit"\
",averlylongnameforafruitcolor"

split( s, a, "," )
for (i=1;i in a;i+=2)
array[a[i]] = a[i+1]


for (k in array)
print k "-->" array[k]

}

Report this thread to moderator Post Follow-up to this message
Old Post
William James
12-02-04 01:55 AM


Re: Want a clean way to populate associative array
Janis Papanagnou <Janis_Papanagnou@hotmail.com> wrote in message news:<col930$rg7$1@online.
de>...
> Eric Pement wrote:
[ ... ] 
>
> BEGIN { FS = "\t"
>    while (getline <"yourfile" >0)
>      arr[$1] = $2
> }

Janis, thanks for giving me such an obvious solution. I never used
such a technique to populate arrays before, and I appreciate you
showing me how it's done.

A big thanks also to John DuBois for his great reference to the awk
"array" library of array functions, which was a tremendous inspiration
and source of learning awk tricks. You folks are great!

--
Eric Pement

Report this thread to moderator Post Follow-up to this message
Old Post
Eric Pement
12-02-04 08:56 PM


Re: Want a clean way to populate associative array

Eric Pement wrote:
> Hello, fellow gawkers ...
>
> I'm looking for a "clean" and legible way to populate a 50-element
> associative array in GNU awk. I like the syntax of Perl or PHP
>
>    "apple"    => "red",
>    "banana"   => "yellow",
>    "orange"   => "orange",

Here's one untested way to get something like that syntax:

gawk 'function map(idxs2vals) {
c = split(idxs2vals,i2vs,"\t")
for (i=1; i<=c; i++)
if (split(i2vs[i],i2v,"=>") == 2)
arr[i2v[1]] = i2v[2]
}
BEGIN{map("\
apple=>red\
banana=>yellow\
orange=>orange\
")

for (idx in arr)
printf "%s\t=> %s\n",idx,arr[idx]
}'

In the above, it uses the tabs at the start of each line of the mappings
as the "line" separator and it uses "=>" as the separator between an
index and it's value. Obviously you can tweak either or both of those
depending on how you envision your input format.

For example, something more like this (also untested) would get you a
bit closer to the specific format you like in perl:

gawk 'function map(idxs2vals) {
c = split(idxs2vals,i2vs,",\t")
for (i=1; i<=c; i++)
if (split(i2vs[i],i2v,"[ \t]*=>[ \t]*") == 2)
arr[i2v[1]] = i2v[2]
}
BEGIN{map("\
apple	=> red,\
banana	=> yellow,\
orange	=> orange,\
")

for (idx in arr)
printf "%s\t=> %s\n",idx,arr[idx]
}'

Regards,

Ed.

Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
12-03-04 08:56 AM


Re: Want a clean way to populate associative array
Eric Pement wrote:
>
> Does anyone know of a pre-written awk function or an inventive way for
> populating hashes or associative arrays that is quite legible? I know
> that I could write
>
>    arr["apple"]    = "red"
>    arr["banana"]   = "yellow"
>    arr["orange"]   = "orange"
>
> but I was also toying with the idea of generating a tab-separated list
> of key-value pairs and having awk slurp up the list and parse it. Any
> ideas?

BEGIN { FS = "\t"
while (getline <"yourfile" >0)
arr[$1] = $2
}


Janis

Report this thread to moderator Post Follow-up to this message
Old Post
Janis Papanagnou
12-07-04 08:59 AM


Re: Want a clean way to populate associative array
In article <227a55e9.0412011052.68a1bc22@posting.google.com>,
Eric Pement <pemente@northpark.edu> wrote:
>Does anyone know of a pre-written awk function or an inventive way for
>populating hashes or associative arrays that is quite legible? I know
>that I could write
>
>   arr["apple"]    = "red"
>   arr["banana"]   = "yellow"
>   arr["orange"]   = "orange"
>
>but I was also toying with the idea of generating a tab-separated list
>of key-value pairs and having awk slurp up the list and parse it. Any
>ideas?

There's a function 'Assign' in ftp://ftp.armory.com/pub/lib/awk/array
that does this.  It may be overkill for your application...

John
--
John DuBois  spcecdt@armory.com  KC6QKZ/AE  http://www.armory.com/~spcecdt/

Report this thread to moderator Post Follow-up to this message
Old Post
John DuBois
12-07-04 08:59 AM


Re: Want a clean way to populate associative array
Eric Pement <pemente@northpark.edu> wrote:
> Hello, fellow gawkers ...
>
> I'm looking for a "clean" and legible way to populate a 50-element
> associative array in GNU awk. I like the syntax of Perl or PHP
>
>    "apple"    => "red",
>    "banana"   => "yellow",
>    "orange"   => "orange",
>
> But I know that it's not native to awk. I suggested a very legible
> method of populating an array of month-names and days-per-month not so
> long ago, but that case was a short example that easily fit on one
> line. This case now is a much longer situation where the keys are 30
> to 50 characters in length.
>
> Does anyone know of a pre-written awk function or an inventive way for
> populating hashes or associative arrays that is quite legible? I know
> that I could write
>
>    arr["apple"]    = "red"
>    arr["banana"]   = "yellow"
>    arr["orange"]   = "orange"
>
> but I was also toying with the idea of generating a tab-separated list
> of key-value pairs and having awk slurp up the list and parse it.

Just do that.

> Any ideas?



--
William Park <opengeometry@yahoo.ca>
Linux solution for data processing.

Report this thread to moderator Post Follow-up to this message
Old Post
William Park
12-07-04 08:59 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

AWK archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:22 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.