Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: Create a hash by spliting string

by arivu198314 (Sexton)
on Feb 10, 2011 at 06:35 UTC ( [id://887349]=note: print w/replies, xml ) Need Help??


in reply to Re: Create a hash by spliting string
in thread Create a hash by spliting string

try the below code,
$string="1:one;2:two;3:three"; %hash=grep(/\w+/, split(/(;|:)/, $string));
Arivu

Replies are listed 'Best First'.
Re^3: Create a hash by spliting string
by davido (Cardinal) on Feb 10, 2011 at 07:59 UTC

    This question seems to be popping up all over the forum here, and people are making it harder than it has to be.

    Perl knows how to turn a flat list into a hash. For example, you can say the following (and it will work):

    my %hash = ( 1 => 'one', 2 => 'two', 3 => 'three', );

    And since the => operator is just a special kind of comma (oversimplification), the code above is pretty much the same as this:

    my %hash = ( '1', 'one', '2', 'two', '3', 'three', );

    And that is exactly the same as this:

    my %hash = ( '1', 'one', '2', 'two', '3', 'three' );

    And wouldn't you know it... that works too, as it should.

    By now the light bulb really ought to be switching on. You can assign a flat list to a hash. Hmm, I wonder how we can convert the string presented by the original poster to a flat list...

    split still comes in handy. But the fact is, it's the only tool you need, and you need it only once. The regexp you used has the defect of using capturing parenthesis, which in the context of a split is often undesirable (unless you really do want to keep the delimiters). You could have used non-capturing parens, as in /(?:;|:)/, but you actually don't need to constrain your alternation at all; there's nothing beyond the simple alternation of /;|:/ But for single-character alternation I prefer just using a character class as in /[;:]/. However, it turns out that in the OP's example string, all of the characters he wants to keep are "word" characters (\w), which means all his delimiters are non-word characters (\W). So the split expression could be further simplified to /\W/. Splitting on non-words will give us a flat list that looks like qw/1 one 2 two 3 three/.

    Putting it all together, the line that does all the work will look like this:

    %hash = split /\W/, $string;

    No need for grep, no need for nested splits, no need for map... just toss out the delimiters and you've got a flat list which can be assigned to a hash.


    Dave

      This question seems to be popping up all over the forum here, and people are making it harder than it has to be.
      Maybe because it's obvious this is homework and the students have apparently put zero thought into it?

      Elda Taluta; Sarks Sark; Ark Arks

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2024-04-23 10:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found