Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

compare 2 arrays of strings and form a table

by sharan (Acolyte)
on Mar 10, 2009 at 17:11 UTC ( [id://749655]=perlquestion: print w/replies, xml ) Need Help??

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

Hi all, I am trying to implement a co-relation problem in perl. I have two arrays i.e. array1 and array2 which contain following values.
array1 array2 read book eat apple book novel apple banana play football tennis football novel mazagine
I want my output as:
Hi all, I am trying to implement a co-relation problem in perl. I have two ar +rays i.e. array1 and array2 which contain following values. <c> array1 array2 array3 array4 read book novel magazine eat apple banana play football tennis
i tried implementing it with simple for loops as:
for($i=0;$i<$arraylength;$i++) { for($j=0;$j<$arraylength;$j++) { if($array2[$i]== $array1[$j]) { $array3[$i] = $array2[$j]; } } } for($i=0;$i<$arraylength;$i++) { for($j=1;$j<$arraylength;$j++) { if($array2[$i]== $array2[$j]) { $array3[$i] = $array1[$j]; } } } for($i=0;$i<$arraylength;$i++) { for($j=0;$j<$arraylength;$j++) { if($array2[$i]== $array1[$j]) { $array4[$i] = $array3[$j]; } } }
But its not showing the proper output. It shows some random output in array3 and array4. Please guide me. Thanking you,

Replies are listed 'Best First'.
Re: compare 2 arrays of strings and form a table
by Roy Johnson (Monsignor) on Mar 10, 2009 at 18:09 UTC
    You're using numerical comparison (=) rather than string comparison (eq), and your loops aren't very Perlish. You don't describe your problem very clearly, but it looks like you want array2 to indicate values that follow corresponding values from array1: book follows read, apple follows eat, etc.

    For the output, the first column should be things that don't appear in array2, and each subsequent column should be the values that follow the values in the previous column. If so, you want to reverse football and tennis in your example.

    I found it made the most sense to work with hashes so I could model the "follows" relationship.

    #!perl use strict; use warnings; my @array1 = qw(read eat book apple play football novel); my @array2 = qw(book apple novel banana football tennis magazine); # The follows relationship my %follow; @follow{@array1} = @array2; # Keeping track of what followers I have already used my %h2 = map {$_=>1} @array2; # Start with things that are not followers my @out = ([grep !exists $h2{$_}, @array1]); # Put followers of the previous column on as a new column while (%h2) { push @out, [@follow{@{$out[-1]}}]; delete @h2{@{$out[-1]}}; } use Data::Dumper; print Dumper(\@out); END { use Data::Dumper; print Dumper(\@out); }

    Caution: Contents may have been coded under pressure.
Re: compare 2 arrays of strings and form a table
by eff_i_g (Curate) on Mar 10, 2009 at 17:24 UTC
    Regarding the "...and form a table" portion of your title: Text::Table. The comparisons were addressed here. If you need further help, please post all of the arrays and their contents—I only see two.
Re: compare 2 arrays of strings and form a table
by ikegami (Patriarch) on Mar 10, 2009 at 18:01 UTC
    use strict; use warnings; my %assocs; while (<DATA>) { my ($k, $v) = split; $assocs{$k} = $v; } my %roots; $roots{$_} = 1 for keys %assocs; $roots{$_} = 0 for values %assocs; my @chains; for my $root (grep $roots{$_}, keys %roots) { my $node = $root; my @chain; while (defined($node)) { push @chain, $node; $node = $assocs{$node}; } push @chains, \@chain; } { local $\ = "\n"; local $, = "\t"; print @$_ for @chains; } __DATA__ read book eat apple book novel apple banana play football tennis football novel mazagine
    eat apple banana play football read book novel mazagine tennis football

    Oops, the output is different than what you asked for. Did I misunderstand the question?

      Ok, this one actually gives the output you asked by ignoring order.
      use strict; use warnings; my %sets; my %owner; while (<DATA>) { my ($p, $q) = sort split; next if $p eq $q; $sets{ $owner{$p} ||= $p }{$p} = 1; $sets{ $owner{$q} ||= $q }{$q} = 1; %{ $sets{$p} } = ( %{ delete( $sets{ $owner{$p} } ) }, %{ delete( $sets{ $owner{$q} } ) }, ); $owner{$p} = $p; $owner{$q} = $p; } { local $\ = "\n"; local $, = "\t"; print(keys(%$_)) for values(%sets); } __DATA__ read book eat apple book novel apple banana play football tennis football novel mazagine
      eat banana apple read book mazagine novel tennis play football

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://749655]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-04-25 06:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found