I have 5 arrays I would like to compare but would like to do it a pair at a time. I initially had gotten help by seeing if I can have a variable in an array. My arrays were: @array1, @array2, @array3, @array4. I am able to compare 2 at a time manually with:
use strict; use warnings; @array1 = (c,d,e); @array2 = (e,f,g,h); @array3 = (a,b,d); @array4 = (s,g,h,j,k,l) my (%union, %intersect); foreach my $e (@array1, @array2) { $union{$e}++ && $intersect{$e}++ } my @intersect = sort keys %intersect; print FILEOUT "@intersect\n"; #prints intersecting words print FILEOUT scalar @intersect;
I thoutht I could change the array names to variable so that I can have the script run through all pairs. The idea was this; through a loop change the array name so that they could be compared. Name change would be done like this: foreach (1..4){@{"array$_"};} I started by testing it out like this:
foreach(1..4) { my (%union, %intersect); foreach my $e (@title1, @{"title$_"}) { $union{$e}++ && $intersect{$e}++ } my @intersect = sort keys %intersect; print FILEOUT "@intersect\n"; print FILEOUT scalar @intersect; print FILEOUT "\n"; }
It wasn't working. For some reason the comparison doesn't work and my results were four 0s. I thought it may have been where I declared the variables and moved it out of the loop but no luck. Wondering if anyone had any ideas or could help. My end result will be a table like this:
array1 : array2 : array3 : array4 array1: 10 : 5 : 17 : 2 array2: 5 : 15 : 8 : 1 array3: 17 : 8 : 14 : 6 array4: 2 : 1 : 6 : 19
Thought I'd start with the comparison first and then format it. I think as I loop through it, I can control the layout. Thanks-

In reply to Array name with a Variable by godevars

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.