Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: hash from CSV-like structure

by jZed (Prior)
on Jan 04, 2008 at 20:56 UTC ( [id://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|_|

Replies are listed 'Best First'.
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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://660468]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found