icarus wrote:
I have two files: log_ca.txt and log_aa.txt
contents of log_ca.txt:

333333333->ca_filename3
444444444->ca_filename4
111111111->ca_filename1
222222222->ca_filename2

contents of log_aa.txt:

111111111->aa_filename1
333333333->aa_filename3
222222222->aa_filename2
444444444->aa_filename4

The program extracts the values after the -> delimiter of both files
Makes an association between the values on both of the files.

Meaning, this is desired output:

CA FILENAME => AA_FILENAME
---------------------------
ca_filename1 => aa_filename1
ca_filename2 => aa_filename2
ca_filename3 => aa_filename3
ca_filename4 => aa_filename4

Suggested code:

    print "CA FILENAME => AA_FILENAME\n";
    print '-' x 27, "\n";

    open my $ca, '<', 'log_ca.txt' or die $!;
    my %ca;
    while ( <$ca> ) {
        chomp;
        my ($k, $v) = split /->/;
        $ca{$k} = $v;
    }

    open my $aa, '<', 'log_aa.txt' or die $!;
    foreach ( sort <$aa> ) {
        chomp;
        my ($k, $v) = split /->/;
        print "$ca{$k} => $v\n";
    }

Outputs I'm getting
(see "double pop" in code below for details)

CA FILENAME => AA_FILENAME
---------------------------
 =>
ca_filename3 => aa_filename3
ca_filename4 => aa_filename4

and

(after adding the "double pop" below prints all records but still get
the => delimiter)
CA FILENAME => AA_FILENAME
---------------------------
 =>
ca_filename1 => aa_filename1
ca_filename2 => aa_filename2
ca_filename3 => aa_filename3
ca_filename4 => aa_filename4


Questions:
How do I get the desired output without resorting to the 'double pop'?

See suggested change below.

How do I get rid of the extra "=>"?

That is also taken care of if you change those lines as suggested.

<snip>

foreach(@ca_files_sorted){
        s/[\r\t\n]+//; #Remove carriage returns and new lines
        my @temp = split (/\d+->/, $_);
        push @ca_filenames, @temp;

That does not do what you think it does. Check the contents of @temp to see what I mean.

You probably want to replace the two latest lines with:

        my ($temp) = /\d+->(.+)/;
        push @ca_filenames, $temp;

foreach(@aa_files_sorted){
        s/[\r\t\n]+//; #Remove carriage returns and new lines
        my @temp = split (/\d+->/, $_);
        push @aa_filenames, @temp;

Ditto.

#why do I need to put [EMAIL PROTECTED] twice?

Because @ca_filenames and @aa_filenames contain empty elements.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to