Re: creating hash from scalar variable
- From: myklass@xxxxxxxxx (Goksie)
- Date: Thu, 03 May 2007 17:33:34 +0100
Matthew J. Avitable wrote:
Unf. Got the picture! I'll spend my night in the stockades :)yes... to apply my code to the actual file....
-m
Rob Dixon wrote:
Matthew J. Avitable wrote:
Given the original string ...
my $test =
'NAS-IP-Address = 192.168.42.1
.......
Acct-Unique-Session-Id = "87d380e1881d226c"
Timestamp = 1177282824';
You could also invoke perl 5.8's ability to treat an in-memory
string as a file:
## get a filehandle on $test
open(my $fh, '<', \$test) or die "Unable to open scalar ref for
reading: $!";
while (my $line = <$fh>) {
Hmm. I don't feel this is an improvement over just
foreach (split /\n/, $line) {
:
}
or even
while ($test =~ /(.*\n?)/g) {
my $line = $1;
:
}
to avoid duplicating the whole string at once.
## split on '=' with a max of two resulting fields, clear spaces
adjacent to '='.
## clear newlines as well.
chomp ( my ($k, $v) = split(/\s*=\s*/, $line, 2) );
Why chomp here? You're chomping $k, which /can't/ end in a record, as
well as $v.
Just
chomp $line;
my ($k, $v) = split /\s*=\s*/, $line, 2;
is more appropriate.
## clear out the quotes in the value
$v =~ s/"//og;
Why use the /o modifier when there are no variables being
interpolated into
the regex?
Even better (in terms of speed) would be
$v =~ tr/"//d;
although I admit I tend to use s/// exclusively myself.
Also why remove the quotes when the OP didn't say he wanted to, and
may actually
need them?
## do something with your key and value:
print "Key is $k, Value is: $v\n";
}
close $fh;
Reading a file line by line is a fairly recognizable pattern to perl
programmers of all levels, so it may assist future maintainers.
...unless it stops working, when opening a string on a filehandle is a
fairly /unrecognisable/ pattern, and the maintainer may be stumped!
Cheers,
Rob
everything was printed okay.. using the code below...
but i need to separate each entry so that when inserting into mysql db,
there wound be mismatch.
#!/usr/bin/perl
#use strict;
use warnings;
my $fn = 'detail-20070423_1.txt';
open $fh,'<', $fn or die $!;
my @array;
while(<$fh>){
if (/^[A-Za-z]{3}\s{1}Apr\s{1}\d{2}\s{1}(\d{2}:?){3}\d{4}/){
print "\n\n\n";
}else{
#if ( !/(^\s+$){2}/ ){
act();
}
}
sub act{
my %h;
foreach (split /\n/, $_) {
my ($key, $val) = split /=/;
$h{$key} = $val;
print "$val";
}
}
thanks for the past help.
Goksie.
EXTRACT OF THE detail-20070423_1.txt is :
Mon Apr 23 00:00:24 2007
NAS-IP-Address = 10.220.42.41
Quintum-NAS-Port = "0 0/0/c1dc2a26"
NAS-Port-Type = Async
User-Name = "10.220.42.38"
Called-Station-Id = "8613508208079"
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 = 0
Acct-Input-Packets = 0
Acct-Output-Packets = 0
Service-Type = Login-User
Quintum-AVPair = "h323-ivr-out=ACCESSCODE:8613508208079"
Quintum-h323-conf-id = "34363262 65383833 32656366 00340000"
Quintum-AVPair = "h323-incoming-conf-id=34363262 65383833 32656366
00340000"
Quintum-h323-gw-id = "mygate"
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 = "10.220.42.38"
Quintum-h323-disconnect-cause = "1f"
Quintum-h323-voice-quality = "0"
Quintum-Trunkid-In = "10.220.42.38"
Quintum-Trunkid-Out = "10.15.115.79"
h323-incoming-conf-id = "34363262 65383833 32656366 00340000"
Client-IP-Address = 10.220.42.41
Acct-Unique-Session-Id = "87d380e1881d226c"
Timestamp = 1177282824
Mon Apr 23 00:00:39 2007
NAS-IP-Address = 10.220.42.41
Quintum-NAS-Port = "0 0/0/c1dc2a26"
NAS-Port-Type = Async
User-Name = "10.220.42.38"
Called-Station-Id = "12508632152"
Calling-Station-Id = ""
Acct-Status-Type = Stop
Acct-Delay-Time = 0
Acct-Input-Octets = 0
Acct-Output-Octets = 0
Acct-Session-Id = "000000C0000012F4"
Acct-Session-Time = 0
Acct-Input-Packets = 0
Acct-Output-Packets = 0
Service-Type = Login-User
Quintum-AVPair = "h323-ivr-out=ACCESSCODE:12508632152"
Quintum-h323-conf-id = "34363262 65383765 32656364 00000000"
Quintum-AVPair = "h323-incoming-conf-id=34363262 65383765 32656364
00000000"
Quintum-h323-gw-id = "mygate"
Quintum-h323-call-origin = "answer"
Quintum-h323-call-type = "VoIP"
Quintum-h323-setup-time = "22:58:05.000 UTC Sun Apr 22 2007"
Quintum-h323-connect-time = "22:59:24.910 UTC Sun Apr 22 2007"
Quintum-h323-disconnect-time = "22:59:24.910 UTC Sun Apr 22 2007"
Quintum-h323-remote-address = "10.220.42.38"
Quintum-h323-disconnect-cause = "1f"
Quintum-h323-voice-quality = "0"
Quintum-Trunkid-In = "10.220.42.38"
Quintum-Trunkid-Out = "10.128.75.204"
h323-incoming-conf-id = "34363262 65383765 32656364 00000000"
Client-IP-Address = 10.220.42.41
Acct-Unique-Session-Id = "2f13e58684844468"
Timestamp = 1177282839
Mon Apr 23 00:00:41 2007
User-Name = ""
NAS-IP-Address = 10.220.42.41
Service-Type = Login-User
Quintum-AVPair = "h323-incoming-conf-id=34363262 65386430 32656431
00000000"
Quintum-AVPair = "h323-ivr-out=ACCESSCODE:13013393338"
Quintum-NAS-Port = "0 0/0/c1dc2a26"
Quintum-h323-conf-id = "34363262 65386430 32656431 00000000"
Quintum-h323-setup-time = "22:59:26.975 UTC Sun Apr 22 2007"
Quintum-h323-call-origin = "answer"
Quintum-h323-call-type = "VoIP"
Quintum-h323-gw-id = "mygate"
Called-Station-Id = "13013393338"
Calling-Station-Id = ""
Acct-Status-Type = Start
Acct-Delay-Time = 0
Acct-Session-Id = "000000C0000012F6"
NAS-Port-Type = Async
h323-incoming-conf-id = "34363262 65386430 32656431 00000000"
Client-IP-Address = 10.220.42.41
Acct-Unique-Session-Id = "e0f0037503ecd771"
Timestamp = 1177282841
Mon Apr 23 00:00:50 2007
User-Name = ""
NAS-IP-Address = 10.220.42.41
Service-Type = Login-User
Quintum-AVPair = "h323-incoming-conf-id=34363262 65386438 32656433
00340000"
Quintum-AVPair = "h323-ivr-out=ACCESSCODE:12508632152"
Quintum-NAS-Port = "0 0/0/c1dc2a26"
Quintum-h323-conf-id = "34363262 65386438 32656433 00340000"
Quintum-h323-setup-time = "22:59:35.665 UTC Sun Apr 22 2007"
Quintum-h323-call-origin = "answer"
Quintum-h323-call-type = "VoIP"
Quintum-h323-gw-id = "mygate"
Called-Station-Id = "12508632152"
Calling-Station-Id = ""
Acct-Status-Type = Start
Acct-Delay-Time = 0
Acct-Session-Id = "000000C0000012F7"
NAS-Port-Type = Async
h323-incoming-conf-id = "34363262 65386438 32656433 00340000"
Client-IP-Address = 10.220.42.41
Acct-Unique-Session-Id = "a93366b14fa83a3e"
Timestamp = 1177282850
Mon Apr 23 00:01:26 2007
NAS-IP-Address = 10.220.42.41
Quintum-NAS-Port = "0 0/0/c1dc2a26"
NAS-Port-Type = Async
User-Name = "10.220.42.38"
Called-Station-Id = "12508632152"
Calling-Station-Id = ""
Acct-Status-Type = Stop
Acct-Delay-Time = 0
Acct-Input-Octets = 0
Acct-Output-Octets = 0
Acct-Session-Id = "000000C0000012F7"
Acct-Session-Time = 0
Acct-Input-Packets = 0
Acct-Output-Packets = 0
Service-Type = Login-User
Quintum-AVPair = "h323-ivr-out=ACCESSCODE:12508632152"
Quintum-h323-conf-id = "34363262 65386438 32656433 00340000"
Quintum-AVPair = "h323-incoming-conf-id=34363262 65386438 32656433
00340000"
Quintum-h323-gw-id = "mygate"
Quintum-h323-call-origin = "answer"
Quintum-h323-call-type = "VoIP"
Quintum-h323-setup-time = "22:59:35.665 UTC Sun Apr 22 2007"
Quintum-h323-connect-time = "23:00:11.755 UTC Sun Apr 22 2007"
Quintum-h323-disconnect-time = "23:00:11.755 UTC Sun Apr 22 2007"
Quintum-h323-remote-address = "10.220.42.38"
Quintum-h323-disconnect-cause = "1f"
Quintum-h323-voice-quality = "0"
Quintum-Trunkid-In = "10.220.42.38"
Quintum-Trunkid-Out = "10.128.75.204"
h323-incoming-conf-id = "34363262 65386438 32656433 00340000"
Client-IP-Address = 10.220.42.41
Acct-Unique-Session-Id = "69512c19e7769b31"
Timestamp = 1177282886
Mon Apr 23 00:03:10 2007
NAS-IP-Address = 10.220.42.41
Quintum-NAS-Port = "0 0/0/c1dc2a26"
NAS-Port-Type = Async
User-Name = "10.220.42.38"
Called-Station-Id = "13013393338"
Calling-Station-Id = ""
Acct-Status-Type = Stop
Acct-Delay-Time = 0
Acct-Input-Octets = 0
Acct-Output-Octets = 0
Acct-Session-Id = "000000C0000012F6"
Acct-Session-Time = 125
Acct-Input-Packets = 0
Acct-Output-Packets = 0
Service-Type = Login-User
Quintum-AVPair = "h323-ivr-out=ACCESSCODE:13013393338"
Quintum-h323-conf-id = "34363262 65386430 32656431 00000000"
Quintum-AVPair = "h323-incoming-conf-id=34363262 65386430 32656431
00000000"
Quintum-h323-gw-id = "mygate"
Quintum-h323-call-origin = "answer"
Quintum-h323-call-type = "VoIP"
Quintum-h323-setup-time = "22:59:26.975 UTC Sun Apr 22 2007"
Quintum-h323-connect-time = "22:59:50.700 UTC Sun Apr 22 2007"
Quintum-h323-disconnect-time = "23:01:55.980 UTC Sun Apr 22 2007"
Quintum-h323-remote-address = "10.220.42.38"
Quintum-h323-disconnect-cause = "10"
Quintum-h323-voice-quality = "0"
Quintum-Trunkid-In = "10.220.42.38"
Quintum-Trunkid-Out = "10.128.75.204"
h323-incoming-conf-id = "34363262 65386430 32656431 00000000"
Client-IP-Address = 10.220.42.41
Acct-Unique-Session-Id = "e9566869177bba83"
Timestamp = 1177282990
Mon Apr 23 00:03:50 2007
User-Name = ""
NAS-IP-Address = 10.220.42.41
Service-Type = Login-User
Quintum-AVPair = "h323-incoming-conf-id=34363262 65393863 32656435
00000000"
Quintum-AVPair = "h323-ivr-out=ACCESSCODE:13074370862"
Quintum-NAS-Port = "0 0/0/c1dc2a26"
Quintum-h323-conf-id = "34363262 65393863 32656435 00000000"
Quintum-h323-setup-time = "23:02:35.570 UTC Sun Apr 22 2007"
Quintum-h323-call-origin = "answer"
Quintum-h323-call-type = "VoIP"
Quintum-h323-gw-id = "mygate"
Called-Station-Id = "13074370862"
Calling-Station-Id = ""
Acct-Status-Type = Start
Acct-Delay-Time = 0
Acct-Session-Id = "000000C0000012F8"
NAS-Port-Type = Async
h323-incoming-conf-id = "34363262 65393863 32656435 00000000"
Client-IP-Address = 10.220.42.41
Acct-Unique-Session-Id = "efed98c811238af1"
Timestamp = 1177283030
Mon Apr 23 00:04:06 2007
NAS-IP-Address = 10.220.42.41
Quintum-NAS-Port = "0 0/0/c1dc2a26"
NAS-Port-Type = Async
User-Name = "10.220.42.38"
Called-Station-Id = "13074370862"
Calling-Station-Id = ""
Acct-Status-Type = Stop
Acct-Delay-Time = 0
Acct-Input-Octets = 0
Acct-Output-Octets = 0
Acct-Session-Id = "000000C0000012F8"
Acct-Session-Time = 0
Acct-Input-Packets = 0
Acct-Output-Packets = 0
Service-Type = Login-User
Quintum-AVPair = "h323-ivr-out=ACCESSCODE:13074370862"
Quintum-h323-conf-id = "34363262 65393863 32656435 00000000"
Quintum-AVPair = "h323-incoming-conf-id=34363262 65393863 32656435
00000000"
Quintum-h323-gw-id = "mygate"
Quintum-h323-call-origin = "answer"
Quintum-h323-call-type = "VoIP"
Quintum-h323-setup-time = "23:02:35.570 UTC Sun Apr 22 2007"
Quintum-h323-connect-time = "23:02:51.445 UTC Sun Apr 22 2007"
Quintum-h323-disconnect-time = "23:02:51.445 UTC Sun Apr 22 2007"
Quintum-h323-remote-address = "10.220.42.38"
Quintum-h323-disconnect-cause = "1f"
Quintum-h323-voice-quality = "0"
Quintum-Trunkid-In = "10.220.42.38"
Quintum-Trunkid-Out = "10.128.75.204"
h323-incoming-conf-id = "34363262 65393863 32656435 00000000"
Client-IP-Address = 10.220.42.41
Acct-Unique-Session-Id = "c494daaf7b7d55c4"
Timestamp = 1177283046
.
- Follow-Ups:
- Re: creating hash from scalar variable
- From: David Van Ginneken
- Re: creating hash from scalar variable
- Prev by Date: Re: Query in pack and unpack functions
- Next by Date: Re: basic perl query
- Previous by thread: basic perl query
- Next by thread: Re: creating hash from scalar variable
- Index(es):
Relevant Pages
|