Re: creating hash from scalar variable

2007-05-03 Thread Dr.Ruud
"David Van Ginneken" schreef: > $fn =~ s/^\s*//g; > $fn =~ s/\s*$//g; > $val =~ s/^\s*"?//g if defined $val; > $val =~ s/"?\s*//g if defined $val; The g-modifiers and the * quantifiers and the "? are either not right or not necessary. Alternative: s/^\s+//, s

Re: creating hash from scalar variable

2007-05-03 Thread David Van Ginneken
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; $/ = "\n\n"; # Specify the record separator as 2 new lines.. my $fn = 'detail-20070423_1.txt'; open my $fh, '<', $fn or die $!; while(<$fh>){ my %test; map { my ($fn,$val) = split(/=/,$_,2); $fn =~ s/^\s*//g;

Re: creating hash from scalar variable

2007-05-03 Thread Goksie
Matthew J. Avitable wrote: > Unf. Got the picture! I'll spend my night in the stockades :) > > -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 = "87d380e1

Re: creating hash from scalar variable

2007-04-30 Thread Matthew J. Avitable
Unf. Got the picture! I'll spend my night in the stockades :) -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

Re: creating hash from scalar variable

2007-04-30 Thread Goksie
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-memo

Re: creating hash from scalar variable

2007-04-30 Thread Rob Dixon
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 $

Re: creating hash from scalar variable

2007-04-30 Thread Randal L. Schwartz
> ""Matthew" == "Matthew J Avitable" <[EMAIL PROTECTED]> writes: "Matthew> You could also invoke perl 5.8's ability to treat an in-memory string as a "Matthew> file: You can, but that's rapidly sliding into "obfuscation" territory. You already have the data... why shove it out as a filehand

Re: creating hash from scalar variable

2007-04-29 Thread Matthew J. Avitable
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

Re: creating hash from scalar variable

2007-04-29 Thread Rob Dixon
Rodrick Brown wrote: use Data::Dumper; my %h; map { $h{$_->[0]}=$_->[1] } map { [ split/=/,$_ ] } split/\n/,$test; print Dumper(\%h); Or, more intelligibly, my %h; foreach (split /\n/, $test) { my ($key, $val) = split /=/; $h{$key} = $val; } Rob -- To unsubscribe, e-mail: [EMAIL P

Re: creating hash from scalar variable

2007-04-29 Thread Rodrick Brown
On 4/29/07, Goksie <[EMAIL PROTECTED]> 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 Use

creating hash from scalar variable

2007-04-29 Thread Goksie
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 =

Re: creating hash from scalar variable

2007-04-29 Thread Martin Barth
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 <[EMAIL PROTECTED]> wrote: > hello, > > Can someone help me correct this code. > > if i print, it only print the first line. > > Goksie > > #!/usr/bin

Re: creating hash from scalar variable

2007-04-29 Thread Rob Dixon
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" Call

Re: creating hash from scalar variable

2007-04-29 Thread yaron
pril 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 =

Re: creating hash from scalar variable

2007-04-29 Thread David Van Ginneken
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 b

Re: creating hash from scalar variable

2007-04-29 Thread Rob Coops
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 m

creating hash from scalar variable

2007-04-29 Thread Goksie
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 =