convenientstore has asked for the wisdom of the Perl Monks concerning the following question:

In perl cook book , I see below as ways to add multiple values to the key
ttys = ( ); open(WHO, "who|") or die "can't open who: $!"; while (<WHO>) { ($user, $tty) = split; push( @{$ttys{$user}}, $tty ); } foreach $user (sort keys %ttys) { print "$user: @{$ttys{$user}}\n"; } The heart
So, I wanted to test it by creating below simple file,
SIP1 whatever 100 failed
SIP1 whatever 200 success
SIP1 whatever 180 success
SIP1 whatever BYE failed
SIP1 whatevers 200 success
SIP2 whatever INVITE sucess
SIP2 whatever 180 success
SIP1 whatever 200 failed
SIP3 whatever 100 trying
SIP4 whatever 503 failed
SIP4 whatever 503 failed
SIP4 whatever 503 failed
SIP1 whatever 183 failed
and I wrote a script as below
#!/usr/bin/perl -w use strict; open FH, 'sipfile.m' or die "can't open file $!"; my ($call1, $sipm); my %data1 = ( ); while (<FH>) { if (($call1,$sipm) = /(SIP[0-9]).+ (\d\d\d).+$/) { push ( @{data1{$call1}}, $sipm ); } } foreach $call1 (sort keys %data1) { print "$data1{$call1}\n"; } close FH;
But when I run it, I get below? is perl cook book wrong ?
or did I do something incorrect?
Scalar value @{data1{$call1} better written as ${data1{$call1} at ./pe +rl.m line 12.<br> Type of arg 1 to push must be array (not hash slice) at ./perl.m line +12, near "$sipm )" Execution of ./perl.m aborted due to compilation errors.

Replies are listed 'Best First'.
Re: multiple values per key
by GrandFather (Saint) on Jul 20, 2007 at 02:51 UTC

    Almost right. You are missing a $ off of data1 in the push. It should be:

    push ( @{$data1{$call1}}, $sipm );

    and your print would be better as:

    print "@{$data1{$call1}}\n";

    which interpolates the array elements into the string. With both changes your code prints:

    100 200 180 200 200 183 180 100 503 503 503

    DWIM is Perl's answer to Gödel
      beautiful. thank you so much
        Now, below program works.. but I still see some uninitialized values
        Here's what I mean. I am running a below code against file that has SIP message, which has various format according to what message and what situation. When I run below code against entire file, it works but exception of some areas where I don't get my value initialized and I suspect that when it goes through line (14-16), it doesn't find the match? and it skips out as I see error message
        Use of uninitialized value at ./perl.m line 32, <FH> chunk 1. Use of uninitialized value at ./perl.m line 32, <FH> chunk 1. Use of uninitialized value at ./perl.m line 32, <FH> chunk 894. Use of uninitialized value at ./perl.m line 32, <FH> chunk 896. Use of uninitialized value at ./perl.m line 32, <FH> chunk 898.
        but line 17 through 38, it recognize as entire record and process what it needs to.. What am I missing in line 14 through 16 that my regular expression is not matching(I want it to match so that it skips out(next record).
        #!/usr/bin/perl -w use strict; my $ncb = $/; my $cbn = $"; $/ = "\n\n"; $" = "\n"; open FH, 'file1' or die "can't open file $!"; my $callid; my $sipm; my %data1 = (); while (<FH>) { chomp; if (m/^###|^\s*$/s) { next ; } m{(^SIP\/2\.0 \d\d\d|^[A-Z]{3,6} ).*(Call-ID: [\S]{25,80})[^ ]+: .*}s; ($sipm,$callid) = ($1,$2); #print "\$sipm is $sipm and \$callid is $callid\n"; push (@{$data1{$callid}}, $sipm); } $/ = $ncb; $" = $cbn; foreach $callid (sort keys %data1) { print "$callid \n", " @{$data1{$callid}}\n"; } close FH;
        top portion of file1 with line number printed out for identification
        14 $ 15 ### sendto xxx.xxx.xxx.xxx7:5060 679 bytes @ Wed Feb 7 14:22: +22 2007$ 16 $ 17 SIP/2.0 200 OK^M$ 18 Via: SIP/2.0/UDP xxx.xxx.xxx.xxx7:5060;branch=xxxxxxxxxxxxxxxx +xxxxxxx;received=xxx.xxx.xxx.xxx7^M$ 19 From: sip:1223456679@xxx.xxx.xxx.xxx7;tag=xxxxxxxxx--116851850 +4^M$ 20 To: sip:18000003333@xxx.xxx.xxx.xxx;tag=ccid-306500088-1-2020^ +M$ 21 Allow: ACK,BYE,CANCEL,INVITE,OPTIONS^M$ 22 Call-ID: Something--662631336@xxx.xxx.xxx.xxx7^M$ 23 CSeq: 1 INVITE^M$ 24 Contact: <sip:xxx.xxx.xxx.xxx:5130>^M$ 25 26 Content-Length: 201^M$ 27 Content-Type: application/sdp^M$ 28 ^M$ 29 v=0^M$ 30 o=50053 306500088 306500088 IN IP4 xx.xx.xx.xx^M$ 31 s=SIP Call^M$ 32 c=IN IP4 xx.xx.xx.xx^M$ 33 t=0 0^M$ 34 m=audio 53776 RTP/AVP 0 11^M$ 35 a=rtpmap:0 PCMU/8000^M$ 36 a=rtpmap:101 telephone-event/8000^M$ 37 a=fmtp:101 0-11^M$ 38 $