Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Split string into hash

by Anonymous Monk
on Feb 10, 2011 at 06:07 UTC ( [id://887343]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks,

I tried the string convert to hash in the following format.

$string="1:one;2:two;3:three";
%hash=(1=>"one", 2=>"two", 4=>"three");

I wrote the code below, but not getting the above hash. Kindly let me know what is the issue, and solution.

use Data::Dumper; %hash = (split /\:/, (split /;/ $string)); print Dumper \%hash;

now getting output is:

$VAR1 = { '3' => undef };

Replies are listed 'Best First'.
Re: Split string into hash
by davido (Cardinal) on Feb 10, 2011 at 06:30 UTC

    You don't have to split twice. Perl is smart enough to know how to load a flat list into a hash.

    use strict; use warnings; use Data::Dumper; my $string = "1:one;2:two;3:three"; my %hash = split /[;:]/, $string; print Dumper \%hash;

    The output is:

    $VAR1 = { '1' => 'one', '3' => 'three', '2' => 'two' };

    No guarantees as to the order, as hashes aren't sorted.


    Dave

Re: Split string into hash
by andreas1234567 (Vicar) on Feb 10, 2011 at 06:19 UTC
    Your code contains syntax errors, and had you used warnings you would have gotten some hints on what is wrong. I would do something like this:
    $ perl use strict; use warnings; use Data::Dumper; my $string="1:one;2:two;3:three"; my %hash; my @list1 = split /;/, $string; foreach my $item(@list1) { my ($i,$j)= split(/:/, $item); $hash{$i} = $j; } print Dumper \%hash; __END__ $VAR1 = { '1' => 'one', '3' => 'three', '2' => 'two' }; $
    --
    No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]
Re: Split string into hash
by Anonymous Monk on Feb 10, 2011 at 06:20 UTC
    %hash = map{split /\:/, $_}(split /;/, $string);
Re: Split string into hash
by chrestomanci (Priest) on Feb 10, 2011 at 11:11 UTC

    We had almost the same homework question yesterday.

    This one at least shows some effort to solve the problem, but I do wonder where the question comes from.

Re: Split string into hash
by sundialsvc4 (Abbot) on Feb 10, 2011 at 15:00 UTC

    Obviously some University or trade-school out there is in its fifth week of classes ... and full of lazy students.   (I have been a college professor, so I can say that.)

    Nevertheless, I would caution that when dealing with “real” data, it isn’t enough to simply write code that works perfectly when given correct data, but that behaves unpredictably at all other times.

    #define SOAPBOX
    • Data will contain errors and inconsistencies, and the only player that can detect them is:   the computer itself.
    • If the computer detects that it is in a situation where it cannot meaningfully continue, it should not allow itself to continue.
    • Only the computer can make that determination.
    • If the software does not aggressively test its data, it is usually impossible to determine whether the problem is with the software or with the data or both.
    • Per contra, if it does do so, it becomes impossible to assert that the input data does not contain the issues that are being tested for ... a very important thing to be able to say in a real-world production setting, where hundred-megabyte input files are common.
    #undef SOAPBOX

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2024-04-19 09:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found