Re: creating hash from scalar variable



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

i have tried all,

and I still stand by my confidence that perl community is still the most
vibrant of all.

Thank you all,

Goksie
.



Relevant Pages

  • Cant Compiling perl5.8.8 on FreeBSD6.2
    ... WITH_DEBUGGING=yes Build perl with debugging support. ... First let's make sure your kit is complete. ... What is the file extension used for shared libraries? ... I'll use sprintf to convert floats into a string. ...
    (comp.unix.bsd.freebsd.misc)
  • Re: use of DBI; I am getting multiple error messages mixed in with ?the correct output.
    ... But I'm not talking about C++ or Java or SQL. ... This is a perl newsgroup. ... to means by a "defined null string", as opposed to an undefined value. ... And where exactly are you getting the idea that the empty string is a ...
    (comp.lang.perl.misc)
  • Re: Perl Strings vs FileHandle
    ... Just wanted to run this through Perl gurus to see if fit is ... testing it every time through the loop. ... The comparison test to the string '1' is superfluous. ... Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers ...
    (comp.lang.perl.misc)
  • Re: Working with Source Code to Insert Copyright Statements as a Header
    ... Paul Lalli wrote: ... Where could I go to find some samples of PERL code that do the ... ascii english text, etc etc etc) at the top of the file, from a string: ... `perldoc Tie::File` at the command line to read its documentation. ...
    (comp.lang.perl.misc)
  • Re: Perl ActiveX: A VBA string passed to my control is treated as a doubel-quoted string
    ... I have written a Perl ActiveX control that's suppose to do some regular ... When I use this string in VBA, I get erroneous resutls from my control. ... Perl regex has its own set of escape characters like any other parser. ...
    (comp.lang.perl.misc)