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

Hi all , I have this form of representation in array (N X 2) for my graph(undirected):-

a b

c d

a d

c a

f g

f h

What i want is its representation in this form my %connections=(a=>[b,d],c=>[d,a],f=>[g,h]);

Replies are listed 'Best First'.
Re: Representing a connection matrix of graph
by aitap (Curate) on Aug 08, 2012 at 12:44 UTC
    Did you write any own code for this?
    use Data::Dumper; my %hash; while (<>) { chomp; my ($what,$where) = split /\s+/,$_; exists $hash{$what} || $hash{$what} = []; push @{$hash{$what}},$where; } print Dumper %hash;
    (untested).
    See perlreftut for more.
    Sorry if my advice was wrong.
Re: Representing a connection matrix of graph
by Kenosis (Priest) on Aug 08, 2012 at 13:05 UTC

    Here's another option:

    use Modern::Perl; use Data::Dumper; my %rep; map {my @x = split; push @{ $rep{ $x[0] } }, $x[1]} grep /\S/, <DATA>; say Dumper \%rep; __DATA__ a b c d a d c a f g f h

    Output:

    $VAR1 = { 'c' => [ 'd', 'a' ], 'a' => [ 'b', 'd' ], 'f' => [ 'g', 'h' ] };

    If there are no blank lines in your data set, you can omit the grep /\S/, in the script.

    Hope this helps!