Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Here is a way that will work, without using modules.
This design is limited to comparing 31 files (assuming a 32 bit machine). Yes - I could extend it to 32 files at the expense of even more obfu.
my @file1 = qw{1 2 3 4 }; my @file2 = qw{ 2 3 4 5}; my %seen; my @fa =([1<<1,\@file1,'File-1 only'],[1<<2,\@file2,'File-2 only'], [1<<1|1<<2,[],'Both have']); for (@fa){ my ($v,$f)=@$_; $seen{$_} |= $v for @$f }; ## print Dumper \%seen; for (@fa){ my ($v,$f,$n)=@$_; $v==$seen{$_} and print qq[$n :$_\n] for sort keys %seen }
Prints:
File-1 only :1 File-2 only :5 Both have :2 Both have :3 Both have :4
Update: Updated code to also print "Both" lines.

     "Man cannot live by bread alone...
         He'd better have some goat cheese and wine to go with it!"


In reply to Re: optimal way of comparing 2 arrays by NetWallah
in thread optimal way of comparing 2 arrays by narashima

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-03-28 14:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found