Re: creating hash from scalar variable
- From: mja@xxxxxxxxxxx (Matthew J. Avitable)
- Date: Sun, 29 Apr 2007 20:55:47 -0400
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>) {
## 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) );
## clear out the quotes in the value
$v =~ s/"//og;
## 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.
Hope this helps!
-m
.
- Follow-Ups:
- Re: creating hash from scalar variable
- From: Rob Dixon
- Re: creating hash from scalar variable
- From: Randal L. Schwartz
- Re: creating hash from scalar variable
- References:
- creating hash from scalar variable
- From: Goksie
- Re: creating hash from scalar variable
- From: Rodrick Brown
- creating hash from scalar variable
- Prev by Date: Re: creating hash from scalar variable
- Next by Date: MAC and install CPAN
- Previous by thread: Re: creating hash from scalar variable
- Next by thread: Re: creating hash from scalar variable
- Index(es):
Relevant Pages
|