Home > Archive > AWK > December 2004 > Want a clean way to populate associative array
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Want a clean way to populate associative array
|
|
| Eric Pement 2004-12-01, 3:55 pm |
| 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
| |
| William Park 2004-12-01, 3:55 pm |
| 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.
| |
| Janis Papanagnou 2004-12-01, 8:55 pm |
| 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
| |
| John DuBois 2004-12-01, 8:55 pm |
| 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/
| |
| William James 2004-12-01, 8:55 pm |
| 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]
}
| |
| Eric Pement 2004-12-02, 3:56 pm |
| 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
| |
| Ed Morton 2004-12-03, 3:56 am |
|
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.
| |
| Janis Papanagnou 2004-12-07, 3:59 am |
| 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
| |
| John DuBois 2004-12-07, 3:59 am |
| 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/
| |
| William Park 2004-12-07, 3:59 am |
| 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.
|
|
|
|
|