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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.