How to initialize an associate array nicely with awk

  • Follow


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:













7/21/2012 2:41:26 AM


Reply: