Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: split question

by George_Sherston (Vicar)
on Sep 20, 2002 at 13:04 UTC ( #199452=note: print w/ replies, xml ) Need Help??


in reply to split question

My first instinct would be to look through CPAN and find a module that parses how you want. Do you have control over the un-parsed form of the data? Then someone else has almost certainly gone through all the thought processes you need to do.

For the particular case you are dealing with, and if you are sure that the form of the input data will always be "space, non-space characters, colon, digits" then you could use a regex thus:

use Data::Dumper; # to let us print out the results at the end my $var = "xxx:12345 yyy:54321 zzz:13245"; my %hash = $var =~ /(\S+):(\d+)/g; # a pattern match in array context returns a list of the matches # and hash is a list in which odd-numbered items are keys and even- # numbered items are values print Dumper(\%hash);
The output is:
$VAR1 = { 'yyy' => '54321', 'xxx' => '12345', 'zzz' => '13245' };
The disadvantage of this is that it does depend on regular input and won't tell you if there is a breakdown in the input, but just spit out rubbish. Better to get a module for general use.

George Sherston


Comment on Re: split question
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2014-12-20 17:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (97 votes), past polls