Some comments about your code:

1) If Grandfather hasn't told you by now, someone else surely will, but you need to use strict; and use warnings;

2) The hash element $AB{$foo} (on lines 8 and 21) is not the same as $Ab{$foo} (on line 20).

3) Your program is either a fragment or else you have forgotten to add data to the $AB/$Ab hash.

4) oshalla is dead on when he suggests a hash.

Let's see if we can make a working example of using a hash. We'll fudge opening two files by loading our "file1" and "file2" with a DATA statement and then using modal behavior to handle both files.

#!/usr/bin/perl use warnings; use strict; my $mode = "start"; my %hash; while(<DATA>) { chomp; if ( $_ =~ /file1/) { $mode = "read"; next; } if ( $_ =~ /file2/) { $mode = "add"; next; } if ( $mode eq "read" ) { $hash{$_} = 0; } elsif ( $mode eq "add" ) { my ( $key, $value ) = split " ", $_, 2; $hash{$key} = $value if ( exists($hash{$key}) ); } } for ( sort keys %hash ) { print "$_ => $hash{$_} \n" if $hash{$_}; } __DATA__ file1 Angel Baker Candy Edward Fox file2 Angel 7 Fox 10 Gigantic 29 Candy 3
And the results are:

C:\Code>perl order_by_key.pl Angel => 7 Candy => 3 Fox => 10

In reply to Re: Compare Arrays, Return Values by dwm042
in thread Compare Arrays, Return Values by de2425

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.