I am trying to initialize an associate array with awk, for example, to
map literal months to numeric months, I did something like:
BEGIN {
mon["Jan"] = 1; .....; mon["Dec"] = 12;
}
Is there some other ways to do this with awk(gawk)?? for example, in
Perl , I can do things like:
my %mon = ();
@mon{ qw/Jan Feb Mar Apr May.... / } = (1..12);
Thanks in advance,
LH
|
|
0
|
|
|
|
Reply
|
lihao0129 (18)
|
12/8/2007 9:57:53 PM |
|
On Sat, 08 Dec 2007 13:57:53 -0800, lihao0129@gmail.com wrote:
> I am trying to initialize an associate array with awk, for example, to
> map literal months to numeric months, I did something like:
>
> BEGIN {
> mon["Jan"] = 1; .....; mon["Dec"] = 12;
> }
>
> Is there some other ways to do this with awk(gawk)?? for example, in
> Perl , I can do things like:
>
> my %mon = ();
> @mon{ qw/Jan Feb Mar Apr May.... / } = (1..12);
>
> Thanks in advance,
> LH
use:
split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec", mon, " ")
--
/home/steffen/usenet-signature
|
|
0
|
|
|
|
Reply
|
Steffen
|
12/8/2007 10:08:17 PM
|
|
On Sat, 08 Dec 2007 22:08:17 +0000, Steffen Schuler wrote:
> On Sat, 08 Dec 2007 13:57:53 -0800, lihao0129@gmail.com wrote:
>
>> I am trying to initialize an associate array with awk, for example, to
>> map literal months to numeric months, I did something like:
>>
>> BEGIN {
>> mon["Jan"] = 1; .....; mon["Dec"] = 12;
>> }
>>
>> Is there some other ways to do this with awk(gawk)?? for example, in
>> Perl , I can do things like:
>>
>> my %mon = ();
>> @mon{ qw/Jan Feb Mar Apr May.... / } = (1..12);
>>
>> Thanks in advance,
>> LH
use:
split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec", mon, " ")
--
|
|
0
|
|
|
|
Reply
|
Steffen
|
12/8/2007 10:10:42 PM
|
|
On Sat, 08 Dec 2007 22:10:42 +0000, Steffen Schuler wrote:
> On Sat, 08 Dec 2007 22:08:17 +0000, Steffen Schuler wrote:
>
>> On Sat, 08 Dec 2007 13:57:53 -0800, lihao0129@gmail.com wrote:
>>
>>> I am trying to initialize an associate array with awk, for example, to
>>> map literal months to numeric months, I did something like:
>>>
>>> BEGIN {
>>> mon["Jan"] = 1; .....; mon["Dec"] = 12;
>>> }
>>>
>>> Is there some other ways to do this with awk(gawk)?? for example, in
>>> Perl , I can do things like:
>>>
>>> my %mon = ();
>>> @mon{ qw/Jan Feb Mar Apr May.... / } = (1..12);
>>>
>>> Thanks in advance,
>>> LH
use:
split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec", mon, " ")
--
Steffen
|
|
0
|
|
|
|
Reply
|
Steffen
|
12/8/2007 10:15:34 PM
|
|
On Sat, 08 Dec 2007 13:57:53 -0800, lihao0129@gmail.com wrote:
> I am trying to initialize an associate array with awk, for example, to
> map literal months to numeric months, I did something like:
>
> BEGIN {
> mon["Jan"] = 1; .....; mon["Dec"] = 12;
> }
>
> Is there some other ways to do this with awk(gawk)?? for example, in
> Perl , I can do things like:
>
> my %mon = ();
> @mon{ qw/Jan Feb Mar Apr May.... / } = (1..12);
>
> Thanks in advance,
> LH
use:
split("Jan Feb Mar Apr May....", moninv, " ")
for (i in mon) mon[moninv[i]] = i
--
Steffen
|
|
0
|
|
|
|
Reply
|
Steffen
|
12/8/2007 10:19:34 PM
|
|
On Dec 8, 5:08 pm, Steffen Schuler <schuler.stef...@googlemail.com>
wrote:
> On Sat, 08 Dec 2007 13:57:53 -0800, lihao0...@gmail.com wrote:
> > I am trying to initialize an associate array with awk, for example, to
> > map literal months to numeric months, I did something like:
>
> > BEGIN {
> > mon["Jan"] = 1; .....; mon["Dec"] = 12;
> > }
>
> > Is there some other ways to do this with awk(gawk)?? for example, in
> > Perl , I can do things like:
>
> > my %mon = ();
> > @mon{ qw/Jan Feb Mar Apr May.... / } = (1..12);
>
> > Thanks in advance,
> > LH
>
> use:
>
> split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec", mon, " ")
>
Hi, Steffen:
thank you for your reply:) but this seems give me an array which has:
mon[1] = "Jan"; mon[2] = "Feb"; .....
what I really want is:
mon["Jan"] = 1; mon["Feb"] = 2; .....
Any other solution?? many thanks:)
LH
> /home/steffen/usenet-signature- Hide quoted text -
>
> - Show quoted text -
|
|
0
|
|
|
|
Reply
|
lihao0129
|
12/8/2007 10:23:40 PM
|
|
On Sat, 08 Dec 2007 14:23:40 -0800, lihao0129@gmail.com wrote:
> On Dec 8, 5:08 pm, Steffen Schuler <schuler.stef...@googlemail.com>
> wrote:
>> On Sat, 08 Dec 2007 13:57:53 -0800, lihao0...@gmail.com wrote:
>> > I am trying to initialize an associate array with awk, for example,
>> > to map literal months to numeric months, I did something like:
>>
>> > BEGIN {
>> > mon["Jan"] = 1; .....; mon["Dec"] = 12;
>> > }
>>
>> > Is there some other ways to do this with awk(gawk)?? for example, in
>> > Perl , I can do things like:
>>
>> > my %mon = ();
>> > @mon{ qw/Jan Feb Mar Apr May.... / } = (1..12);
>>
>> > Thanks in advance,
>> > LH
>>
>> use:
>>
>> split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec", mon, " ")
>>
>>
> Hi, Steffen:
> thank you for your reply:) but this seems give me an array which has:
>
> mon[1] = "Jan"; mon[2] = "Feb"; .....
>
> what I really want is:
>
> mon["Jan"] = 1; mon["Feb"] = 2; .....
>
> Any other solution?? many thanks:)
>
> LH
>
>> /home/steffen/usenet-signature- Hide quoted text -
>>
>> - Show quoted text -
see also my last post.
use:
split("Jan Feb Mar Apr May....", moninv, " ")
for (i in mon) mon[moninv[i]] = i
--
Steffen
|
|
0
|
|
|
|
Reply
|
Steffen
|
12/8/2007 10:25:15 PM
|
|
On Dec 8, 5:19 pm, Steffen Schuler <schuler.stef...@googlemail.com>
wrote:
> On Sat, 08 Dec 2007 13:57:53 -0800, lihao0...@gmail.com wrote:
> > I am trying to initialize an associate array with awk, for example, to
> > map literal months to numeric months, I did something like:
>
> > BEGIN {
> > mon["Jan"] = 1; .....; mon["Dec"] = 12;
> > }
>
> > Is there some other ways to do this with awk(gawk)?? for example, in
> > Perl , I can do things like:
>
> > my %mon = ();
> > @mon{ qw/Jan Feb Mar Apr May.... / } = (1..12);
>
> > Thanks in advance,
> > LH
>
> use:
>
> split("Jan Feb Mar Apr May....", moninv, " ")
> for (i in mon) mon[moninv[i]] = i
>
yeah, this worked, thanks a lot :-)
LH
|
|
0
|
|
|
|
Reply
|
lihao0129
|
12/8/2007 10:28:09 PM
|
|
On Sat, 08 Dec 2007 13:57:53 -0800, lihao0129@gmail.com wrote:
> I am trying to initialize an associate array with awk, for example, to map
> literal months to numeric months, I did something like:
>
> BEGIN {
> mon["Jan"] = 1; .....; mon["Dec"] = 12;
> }
> }
> Is there some other ways to do this with awk(gawk)?? for example, in Perl
> , I can do things like:
>
> my %mon = ();
> @mon{ qw/Jan Feb Mar Apr May.... / } = (1..12);
If the idea is to convert three character month names to numbers, a simple
approach not using an array is illustrated by this test script:
BEGIN{
MonthString = " janfebmaraprmayjunjulaugsepoctnovdec"
}
{
Month = index( MonthString, tolower( $0 ) ) /3
print Month
}
It's not case sensitive.
--
T.E.D. (tdavis@umr.edu) UMR becomes MST soon.
|
|
0
|
|
|
|
Reply
|
Ted
|
12/9/2007 1:56:55 AM
|
|
On Sat, 08 Dec 2007 19:56:55 -0600, Ted Davis <tdavis@umr.edu> wrote:
>On Sat, 08 Dec 2007 13:57:53 -0800, lihao0129@gmail.com wrote:
>
>> I am trying to initialize an associate array with awk, for example, to map
>> literal months to numeric months, I did something like:
>>
>> BEGIN {
>> mon["Jan"] = 1; .....; mon["Dec"] = 12;
>> }
>> }
>> Is there some other ways to do this with awk(gawk)?? for example, in Perl
>> , I can do things like:
>>
>> my %mon = ();
>> @mon{ qw/Jan Feb Mar Apr May.... / } = (1..12);
>
>If the idea is to convert three character month names to numbers, a simple
>approach not using an array is illustrated by this test script:
>
>BEGIN{
> MonthString = " janfebmaraprmayjunjulaugsepoctnovdec"
>}
>{
> Month = index( MonthString, tolower( $0 ) ) /3
> print Month
>}
>
>It's not case sensitive.
I use an array like this:
# make month name to number lookup
n = split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec", k)
for (i = 1; i <= n; i++) month[k[i]] = i
--Grant.
--
http://bugsplatter.mine.nu/
|
|
0
|
|
|
|
Reply
|
Grant
|
12/9/2007 2:16:31 AM
|
|
On Sun, 09 Dec 2007 13:16:31 +1100, Grant wrote:
> On Sat, 08 Dec 2007 19:56:55 -0600, Ted Davis <tdavis@umr.edu> wrote:
>
>>On Sat, 08 Dec 2007 13:57:53 -0800, lihao0129@gmail.com wrote:
>>
>>> I am trying to initialize an associate array with awk, for example, to
>>> map literal months to numeric months, I did something like:
>>>
>>> BEGIN {
>>> mon["Jan"] = 1; .....; mon["Dec"] = 12;
>>> }
>>> }
>>> Is there some other ways to do this with awk(gawk)?? for example, in
>>> Perl , I can do things like:
>>>
>>> my %mon = ();
>>> @mon{ qw/Jan Feb Mar Apr May.... / } = (1..12);
>>
>>If the idea is to convert three character month names to numbers, a
>>simple approach not using an array is illustrated by this test script:
>>
>>BEGIN{
>> MonthString = " janfebmaraprmayjunjulaugsepoctnovdec"
>>}
>>{
>> Month = index( MonthString, tolower( $0 ) ) /3 print Month
>>}
>>}
>>It's not case sensitive.
>
> I use an array like this:
>
> # make month name to number lookup
> n = split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec", k) for (i =
> 1; i <= n; i++) month[k[i]] = i
But to use it to associate month strings with their numbers, you have to
do something like
for( x in k ) if( tolower( $0 ) == tolower( k[x] ) ) do something
BTW, I usually use
Month = index( MonthString, tolower( substr($0, 1, 3) ) ) /3 print Month
so it can accomodate full names as well as abbreviations.
--
T.E.D. (tdavis@umr.edu) UMR becomes MST soon.
|
|
0
|
|
|
|
Reply
|
Ted
|
12/9/2007 4:52:12 PM
|
|
|
10 Replies
1525 Views
(page loaded in 0.094 seconds)
Similiar Articles: Local array variables in functions - comp.lang.awkHow to initialize an associate array nicely with awk - comp.lang ... Local array variables in functions - comp.lang.awk... are passed 'by reference' in awk, and changing ... f77 and dynamic arrays in common blocks - comp.lang.fortran ...How to initialize an associate array nicely with awk - comp.lang ... f77 and dynamic arrays in common blocks - comp.lang.fortran ..... don't fit in with dynamic arrays at ... bit fields and default initialization - comp.lang.c++.moderated ...How to initialize an associate array nicely with awk - comp.lang ... bit fields and default initialization - comp.lang.c++.moderated ..... since code is private - I hope I ... Simple parsing text , but not for newbie - comp.lang.awk ...... it in > a hash/associative array like this: $hash{Porsche_model}=$1 Well, the simplest equivalent of that rule in AWK is ... implementation of associative arrays in AWK ... how to transpose large matrix? - comp.unix.shell> > Well, to nitpick a bit, tail can certainly be told to count bytes rather ... As a simple form, use awk's associative array: a[row "," column]=value; In perl (which ... find sequences of 1's - comp.soft-sys.matlab> > mb well... should have been said at the ... height and width arraysize=size(m); %Initialize res array ... splitting a sequence of arrays - comp.lang.awk Since awk doesn't ... Some text processing questions - comp.lang.vhdlNot nicely, but possible. Your string ... I have another practical need - initialize a constant array of ... You can also implement associative arrays in this way. Filling in the space between two parallel lines with a color ...I saw an area function as well, and it works well. I changed my two parallel lines to just an array, going from 0 to 1 ... space between two words.... - comp.lang.awk ... Print Table of values - comp.soft-sys.math.scilab... table of x and y values in that window as well ... www.math.fsu.edu/~bellenot Professor and Associate ... awk challenge: sort array using only for ... in ... - comp.lang ... Could anyone give me the spice-mode.el - comp.emacsHi, All I am new to *NIX and I am thinking of writing spice code under Emacs. However, I have no idea of Emacs Lisp. Hence, I could not write a packa... How to initialize an associate array nicely with awk - Page 2How to initialize an associate array nicely with awk - awk . This is a discussion on How to initialize an associate array nicely with awk - awk; On Sun, 09 Dec 2007 ... The GNU Awk User's Guide - Arrays in awk - LinuxSelfhelp - Need ...It also describes how @command{awk} simulates multidimensional arrays, as well ... Arrays in @command{awk} are different--they are associative. This means that each array is ... 7/21/2012 2:41:26 AM
|