Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Unable to extract value from hash table

by tybalt89 (Monsignor)
on Jun 23, 2018 at 15:05 UTC ( [id://1217281]=note: print w/replies, xml ) Need Help??


in reply to Unable to extract value from hash table

Leading space!

Change my @line = split (/,/,$line); to my @line = split (/, */,$line);

Replies are listed 'Best First'.
Re^2: Unable to extract value from hash table
by NsshB (Initiate) on Jun 23, 2018 at 18:12 UTC

    Thanks for the help. My gut told me it was a simple fix but I just couldn't see it.

Re^2: Unable to extract value from hash table
by sundialsvc4 (Abbot) on Jun 25, 2018 at 18:47 UTC

    Perhaps a slightly-safer split-regex might be: /\s*,\s*/ or something along those lines.

    This splits on “a comma, preceded and followed by zero-or-more whitespace characters.”   So the match will occur whether-or-not the comma is surrounded on either side by whitespace, and will “eat” both the comma and the whitespace.

    $ perl -e 'use Data::Dumper; my $a = "a, b , c,d"; my @b = split(/\s*, +\s*/, $a); print Data::Dumper->Dump([\@b], ['b']);' $b = [ 'a', 'b', 'c', 'd' ];
    but also note ...
    $ perl -e 'use Data::Dumper; my $a = " a, b , c,d "; my @b = split(/\s +*,\s*/, $a); print Data::Dumper->Dump([\@b], ['b']);' $b = [ ' a', 'b', 'c', 'd ' ];
    (Which is a valid and correct parse since the example string contains leading and trailing spaces, which split of course does not care about.)

    Therefore, an equally good possible strategy therefore would be to split on /,/ alone, then separately remove leading and/or trailing whitespace from each piece.   Or, remove any leading and/or trailing whitespace from the entire line before splitting it up as described.   If you do not care to install String::Util to get access to a trim() function, you can also use this Perl-foo to remove leading and trailing whitespace:  $str =~ s/^\s+|\s+$//g

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1217281]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-25 15:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found