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 locked_user 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