Re: creating hash from scalar variable



Hi there,

Your problem here is that perl is kind enough to see the whole scalar as one
line. So what you would have to do is break it up in several linse shove
them in an array. Then do a foreach on the array to split and drop it in the
hash like so:

my @array = split( /\n/, $test );
foreach my $line ( @array ) {
chomp( $line );
my ( $fname, $fvalu ) = split( /=/, $line );
print "$fname ----- $fvalu\n";
}


This will do the trick... the chomp is not realyea needed by the way, as I
am sure someone will be pointing out in the near future and I'm ptretty sure
you could make this into a one liner which would be faster but for
readability this is the simplest way of doing it...

Regards,

Rob



On 4/29/07, Goksie <myklass@xxxxxxxxx> wrote:

hello,

Can someone help me correct this code.

if i print, it only print the first line.

Goksie

#!/usr/bin/perl
use strict;

my $test =
'NAS-IP-Address = 192.168.42.1
Quintum-NAS-Port = "0 0/0/c1dc2a26"
NAS-Port-Type = Async
User-Name = "192.168.42.8"
Called-Station-Id = "8600508208079"
Calling-Station-Id = ""
Acct-Status-Type = Stop
Acct-Delay-Time = 0
Acct-Input-Octets = 0
Acct-Output-Octets = 0
Acct-Session-Id = "000000C0000012F5"
Acct-Session-Time = 245
Acct-Input-Packets = 0
Acct-Output-Packets = 0
Service-Type = Login-User
Quintum-AVPair = "h323-ivr-out=ACCESSCODE:8600508208079"
Quintum-h323-conf-id = "34363262 65383833 32656366 00340000"
Quintum-AVPair = "h323-incoming-conf-id=34363262 65383833 32656366
00340000"
Quintum-h323-gw-id = "ng-la"
Quintum-h323-call-origin = "answer"
Quintum-h323-call-type = "VoIP"
Quintum-h323-setup-time = "22:58:10.220 UTC Sun Apr 22 2007"
Quintum-h323-connect-time = "22:59:09.550 UTC Sun Apr 22 2007"
Quintum-h323-disconnect-time = "22:59:09.550 UTC Sun Apr 22 2007"
Quintum-h323-remote-address = "192.168.42.8"
Quintum-h323-disconnect-cause = "1f"
Quintum-h323-voice-quality = "0"
Quintum-Trunkid-In = "192.20.42.8"
Quintum-Trunkid-Out = "10.15.115.79"
h323-incoming-conf-id = "34363262 65383833 32656366 00340000"
Client-IP-Address = 192.168.42.1
Acct-Unique-Session-Id = "87d380e1881d226c"
Timestamp = 1177282824';

my %test = my($fname, $fvalu)=split(/=/, $test);
foreach(keys %test)
{
print "$_ --- $test{$_}";
}

OUTPUT
NAS-IP-Address --- 192.168.42.1
Quintum-NAS-Port ---



--
To unsubscribe, e-mail: beginners-unsubscribe@xxxxxxxx
For additional commands, e-mail: beginners-help@xxxxxxxx
http://learn.perl.org/





Relevant Pages