Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: hash from CSV-like structure

by jZed (Prior)
on Jan 04, 2008 at 20:56 UTC ( #660468=note: print w/ replies, xml ) Need Help??


in reply to hash from CSV-like structure

I'm not really sure what your criteria for duplicates is, but this produces the results you requested from the data you gave:

#!/usr/bin/perl use warnings; use strict; use Data::Dumper; my $hash; my %results; for my $row( split("\n",join('',<DATA>)) ){ my($key1,$key2,$val) = split('\|_\|',$row); next if $results{$val} and $hash->{$key1}->{$key2}; $hash->{$key1}->{$key2}=$val; $results{$val}=1; } print Dumper $hash; __DATA__ 200326951|_|rel_Access1|_|200315482|_| 200326951|_|rel_Access1|_|200315786|_| 200326951|_|rel_Access2|_|200315482|_| 200326951|_|rel_Access2|_|200315786|_|


Comment on Re: hash from CSV-like structure
Download Code
Re^2: hash from CSV-like structure
by manav_gupta (Acolyte) on Jan 05, 2008 at 11:51 UTC
    Super, many thanks jZed - that works!

      To avoid undef warnings, you might want to make the duplicate check more like this:
      next if $results{$val} and $hash->{$key1} and $hash->{$key1}->{$key2};
Re^2: hash from CSV-like structure
by manav_gupta (Acolyte) on Jan 05, 2008 at 14:17 UTC
    Thanks - this fits my needs exactly!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (10)
As of 2015-07-06 16:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (77 votes), past polls