calebcall has asked for the wisdom of the Perl Monks concerning the following question:

Hashes are a new realm for me, so I'm certain someone will be able to explain to me how simple this really is, but I've been trying and can't get it right. I have a string that I would like split in to a hash. I've been trying to use split, but maybe that's not the best way to do it. The string would look something like the following:

$info = "word1": 12345, "word2": true, "another_word": true, "something_else": ["Something", "Some 2", "http://www.website.com"], "more": "5"

Of course, those are all made up values, but the point is, it's not always a word => number pairing, word => word, etc. It can be any combo and each keys element could be one value or multiple, comma separated values. What I would like to split the example up to would be:

"word1" => 12345, "word2" => true, "another_word" => true, "something_else" => ["Something", "Some 2", "http://www.website.com"], "more" => "5"

The split regex I was trying to use was '":\ ', so my code looked something like this:

%info = split (/(":\ )/, $info);

That isn't working, any suggestions?

Replies are listed 'Best First'.
Re: Trying to build a hash from a string...
by hdb (Monsignor) on Nov 09, 2013 at 09:18 UTC

    Your string looks very much like JSON format. If you add braces around it, you can use the JSON module and are done:

    use strict; use warnings; use Data::Dumper; use JSON; my $info = '"word1": 12345, "word2": true, "another_word": true, "some +thing_else": ["Something", "Some 2", "http://www.website.com"], "more +": "5"'; my $hash = from_json '{'.$info.'}'; print Dumper $hash;

      That's exactly what it was. Using the JSON module did exactly what I was looking for. Thanks

Re: Trying to build a hash from a string...
by GrandFather (Saint) on Nov 09, 2013 at 09:02 UTC

    It would help us a lot if you put all your pieces together in a short script that demonstrates what you have tried and how it fails. "isn't working" doesn't actually tell us much. To get some idea of how your script might look consider:

    use warnings; use strict; my $str = "word1: 12345, word2: true"; my %hash = split /[:,]\s*/, $str; print "$_: $hash{$_}\n" for sort keys %hash;

    Prints:

    word1: 12345 word2: true

    A couple of notes:

    1. Always use strictures (use strict; use warnings;). That incantation turns on some highly valuable error checking which will save you a huge amount of debugging time.
    2. For illustrating the hash stuff I've punted on parsing the complicated stuff in your sample data - that's a different problem and one problem at a time is smart.
    True laziness is hard work
Re: Trying to build a hash from a string...
by ww (Archbishop) on Nov 09, 2013 at 12:18 UTC
    The split regex I was trying to use was '":\ ', so my code looked something like this:
    %info = split (/(":\ )/, $info);

    Looks like a refresher on split (perldoc -f split, for example) is in order along with a review of regex construction: escaping a literal space, "/\ /", makes no sense.

      ... escaping a literal space, "/\ /", makes no sense.

      It's pointless in this case, but it does no harm.

      %info = split (/(":\ )/, $info);

      However, putting the split pattern into a capture group  /(":\ )/ causes the separator sub-strings to be returned along with the stuff one might possibly want, resulting, in this case, in a bunch of extraneous (Update: and confounding)  '": ' thingies in the output list.