Dear Monks,

Is there another way to have the intersection of two lists ? Thanks a lot

Here is my code

#usr/bin/perl use strict; my $name_1 = q{list_1.txt}; my $name_2 = q{list_2.txt}; open my $INFILE_1, q{<}, $name_1 or die "Can't open $name_1 : $!"; open my $INFILE_2, q{<}, $name_2 or die "Can't open $name_2 : $!"; my @list_1; while (my $line_1 = <$INFILE_1>){ $line_1 =~ s/\s+$//; push @list_1, $line_1; } my @list_2; while (my $line_2 = <$INFILE_2>){ $line_2 =~ s/\s+$//; push @list_2, $line_2; } my @intersection; my %count; my $element; my @two_lists = (@list_1,@list_2); print STDOUT "@two_lists\n"; foreach $element(@two_lists){ $count{$element}++; } my $key; my @KEYS = keys %count; foreach $key(@KEYS){ if ($count{$key} == 1){ print STDOUT "$key;$count{$key}\n\n"; } } close $INFILE_1; close $INFILE_2;
Here is the first file
car train plane
Here is the second file
boat car train

In reply to is there a more simple way to have the intersection of two lists ? by steph_bow

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.