Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this messageEric 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.
Post Follow-up to this messageEric 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
Post Follow-up to this messageIn 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/
Post Follow-up to this messageBEGIN {
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]
}
Post Follow-up to this messageJanis 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
Post Follow-up to this message
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.
Post Follow-up to this messageEric 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
Post Follow-up to this messageIn 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/
Post Follow-up to this messageEric 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.
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.