Home > Archive > PERL Beginners > April 2007 > creating hash from scalar variable
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
creating hash from scalar variable
|
|
| Goksie 2007-04-29, 6:58 pm |
| 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 ---
| |
| Rob Coops 2007-04-29, 6:58 pm |
| 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@gmail.com> 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@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>
| |
| David Van Ginneken 2007-04-29, 6:58 pm |
| I think something like this would work for you.
my %test;
map { my ($fn,$val) = split(/=/,$_,2); $test{$fn}=$val;} split(/\n/, $test);
I noticed some of your values had equal signs in them, so in the inside
split, I also specified you wanted 2 values so that you receive the full
expected value back.
Hope this helps.
-Dave
On 4/29/07, Goksie <myklass@gmail.com> 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@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>
| |
| yaron@kahanovitch.com 2007-04-29, 6:58 pm |
|
Hi,
The line --> my %test = my($fname, $fvalu)=split(/=/, $test);
Will insert only two elements into %test.
Try:
my %test = split (/=/,$test);
Yaron Kahanovitch
----- Original Message -----
From: "Goksie" <myklass@gmail.com>
To: "Perl Beginners" <beginners@perl.org>
Sent: Sunday, April 29, 2007 4:27:52 PM (GMT+0200) Auto-Detected
Subject: creating hash from scalar variable
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@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
| |
| Rob Dixon 2007-04-29, 6:58 pm |
| Goksie 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 ---
Hope this helps,
Rob
my %test = $test =~ /( "[^"]+" | [^=\s]+ )/gx;
foreach (keys %test) {
print "$_ --- $test{$_}\n";
}
**OUTPUT**
NAS-Port-Type --- Async
Quintum-NAS-Port --- "0 0/0/c1dc2a26"
Acct-Unique-Session-Id --- "87d380e1881d226c"
Called-Station-Id --- "8600508208079"
Quintum-h323-voice-quality --- "0"
Quintum-h323-remote-address --- "192.168.42.8"
Client-IP-Address --- 192.168.42.1
Quintum-h323-setup-time --- "22:58:10.220 UTC Sun Apr 22 2007"
Acct-Status-Type --- Stop
Acct-Output-Packets --- 0
Quintum-h323-disconnect-cause --- "1f"
NAS-IP-Address --- 192.168.42.1
Acct-Output-Octets --- 0
Acct-Session-Time --- 245
h323-incoming-conf-id --- "34363262 65383833 32656366 00340000"
Quintum-AVPair --- "h323-incoming-conf-id=34363262 65383833 32656366 00340000"
Timestamp --- 1177282824
User-Name --- "192.168.42.8"
Acct-Input-Packets --- 0
Acct-Input-Octets --- 0
Acct-Session-Id --- "000000C0000012F5"
Service-Type --- Login-User
Quintum-h323-gw-id --- "ng-la"
Quintum-h323-call-type --- "VoIP"
Quintum-Trunkid-Out --- "10.15.115.79"
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-Trunkid-In --- "192.20.42.8"
Calling-Station-Id --- ""
Quintum-h323-call-origin --- "answer"
Quintum-h323-conf-id --- "34363262 65383833 32656366 00340000"
Acct-Delay-Time --- 0
| |
| Martin Barth 2007-04-29, 6:58 pm |
| Hi,
if you're reading a config file to get the string maybe Config::General is handy.
HTH
Martin
On Sun, 29 Apr 2007 14:27:52 +0100
Goksie <myklass@gmail.com> 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 ---
>
>
>
|
|
|
|
|