Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: How to save first two columns of an array into another array

by toolic (Bishop)
on Oct 01, 2012 at 15:41 UTC ( #996680=note: print w/ replies, xml ) Need Help??

in reply to How to save first two columns of an array into another array

Grab a slice of each array in @S:

use strict; use warnings; use Data::Dumper; my @S; while (<DATA>) { push @S, [split]; } print "-----TRIPLETS-------\n"; print Dumper \@S; my @new = map { [@{ $_ }[0 .. 1]] } @S; print Dumper \@new; __DATA__ b c a a c d d e b


-----TRIPLETS------- $VAR1 = [ [ 'b', 'c', 'a' ], [ 'a', 'c', 'd' ], [ 'd', 'e', 'b' ] ]; $VAR1 = [ [ 'b', 'c' ], [ 'a', 'c' ], [ 'd', 'e' ] ];

Comment on Re: How to save first two columns of an array into another array
Select or Download Code
Replies are listed 'Best First'.
Re^2: How to save first two columns of an array into another array
by zing (Beadle) on Oct 02, 2012 at 14:05 UTC
    Ok and how do I read the same (i.e. those first two columns into a hash). this is what I tried.
    my %h; my @a = map { [@{ $_ }[0]] } @S; my @b = map { [@{ $_ }[1]] } @S; @h{@a} = @b; print Dumper(@a); print Dumper(@b); print Dumper(%h); print "or\n"; print Dumper(\%h);
    But the output is wrong
    $VAR1 = { 'ARRAY(0x8411210)' => [ 'e' ], 'ARRAY(0x840d768)' => [ 'c' ], 'ARRAY(0x83f3308)' => [ 'c' ] };

      Is this what you wanted?

      my %h = map { $_->[0] => $_->[1] } @S; print Dumper(\%h);


      $VAR1 = { 'a' => 'c', 'b' => 'c', 'd' => 'e' };

      Note: Once you put the data into a hash, the original order of the keys is lost. This is the way hashes work.

      Hope that helps,

      Athanasius <°(((><contra mundum

        Thanks all. So after introducing corrections suggested by you,I achieved at this code below. It operates on the same data file and outputs the connected components.But the problem is that the last part of the code i.e. the connected components part isnt working well.

        If I enable "use strict" i get this error :- "Can't use string ("c") as an ARRAY ref while "strict refs" in use at line 44, <> line 3." Line 44 being this "for my $tgt ( \@{ $h{$src} } ) {"

        Whereas if I disable "use strict" i get incomplete output for the connected components as :

        ----connected components--------------- - d: [] - a: [] b: []
        The correct output should be this
        ----connected components--------------- - d: [e] - a: [c] b: [c]
        My second question is that how can I access each of these connected components separately as per my need(i.e. either of them).
        #This program read the triplets from file named "data" into #an array of array. #use strict; use warnings; use Data::Dumper; use Graph; use Graph::Subgraph; # use PDL; my @S; while (<>) { push @S, [ split ]; } print "#########TRIPLETS#####\n"; print Dumper \@S; print "\n$S[0][1]\n"; # Find the number of vertices my @L; for my $i ( 0 .. $#S ) { for my $j ( 0 .. $#{ $S[$i] } ) { #print "elt $i $j is $S[$i][$j]\n"; push (@L,$S[$i][$j]); } } my %seen; @L = grep { ! $seen{ $_ }++ } @L; print "########VERTICES##########\n"; print Dumper \@L; # Now lets generate the G(L) # In order to generate the G(L) we'll extract first two columns of S i +nto another matrix my @GL=@S; splice(@$_, 2, 1) foreach @GL; print Dumper \@GL; my %h = map { $_->[0] => $_->[1] } @S; print Dumper(\%h); ##### CONNECTED COMPONENTS ########## my $g = Graph->new( undirected => 1 ); for my $src ( keys %h ) { for my $tgt ( \@{ $h{$src} } ) { $g->add_edge($src, $tgt); } } my @subgraphs = $g->connected_components; my @allgraphs; my $V = $g->vertices; print "\n$V\n"; for my $subgraph ( @subgraphs ) { push @allgraphs, {}; for my $node ( @$subgraph ) { if ( exists $h{ $node } ) { $allgraphs[-1]{$node} = [ @{ $h{$node} } ]; } } } print "----connected components------------"; use YAML; print Dump \@allgraphs;
      In your code above, you are creating @a & @b as arrays of references to (one element) arrays. Perhaps what you intend is something like:
      my @a = map { ${ $_ }[0] ] @S;
Re^2: How to save first two columns of an array into another array
by Anonymous Monk on Oct 03, 2012 at 15:39 UTC
    Hi guys, there's one more thing Im stuck with. Suppose I have these points=(a,b,c,d);

    Then I want to find the set of triplets induced by these 4 vertices.

    For example for above four points the induced triplets should be:
    b c a a c d
    Whereas for vertices=(d,e,a) there isn't any triplet in the data.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (12)
As of 2016-02-12 19:11 GMT
Find Nodes?
    Voting Booth?

    How many photographs, souvenirs, artworks, trophies or other decorative objects are displayed in your home?

    Results (412 votes), past polls