Hello Anonymous Monk,

I can see that everyone has all ready proposed answers and probably all of them are working. I just could not resist to the idea of trying also to create my own answer since I think it a useful topic comparing arrays.

For my answer I use Data::Compare module which provides you which can provide you with several different outputs when you compare arrays. I have also combine my answer with a perlre (regular expression) taken from Regex To Remove File Extension which makes your solution more generic and not only to *.txt extensions.

I would recommend to modify the title to something more appropriate (e.g. Comparing arrays with regex). This is because it produces confusion to people in future that are looking of simply comparing arrays without file extensions etc. etc.

Having said that, sample of working code with output:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; sub array_utils { use Array::Utils qw(:all); my @compare = (); my @account = qw( 96329XY 7776fc 334457yg 2233oop 000001 8877666b) +; my @file = qw(8877666b.txt 2233oop.txt 334457yg.txt 7776fc.txt 96 +329XY_K.txt); foreach my $sample (@file) { $sample =~ s/(.+)\.[^.]+$/$1/; push @compare,$sample; } # symmetric difference return my @diff = array_diff(@account, @compare); # intersection #return my @isect = intersect(@account, @compare); # unique union #return my @unique = unique(@account, @compare); # check if arrays contain same members if ( !array_diff(@account, @compare) ) { return "Arrays contain same members!"; } # get items from array @account that are not in array @compare #return my @minus = array_minus( @account , @compare ); } my @final = array_utils(); print Dumper(\@final); __END__ $VAR1 = [ '96329XY', '000001', '96329XY_K' ];

Play around by removing the # symbols and observe the output, maybe one of them fits more to your needs.

Seeking for Perl wisdom...on the process of learning...not there...yet!

In reply to Re: Compare two arrays by thanos1983
in thread Compare two arrays by Anonymous Monk

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.