Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
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 having an uproarious good time at the Monastery: (9)
As of 2014-08-20 06:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (105 votes), past polls