develooper Front page | perl.beginners | Postings from April 2008

Re: Properly displaying items from hash

Thread Previous
From:
J. Peng
Date:
April 24, 2008 04:09
Subject:
Re: Properly displaying items from hash
On Thu, Apr 24, 2008 at 9:35 AM, icarus <rsarpi@gmail.com> 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
>


It's may easy or not, based on the special cases you have.
I could show one of the ways:

use strict;
use warnings;

my (%hash, %hash2);

open my $hd, 'log_aa.txt' or die $!;
while(<$hd>) {
    next if /^$/;
    chomp;
    my ($id) = /(\d+)$/;
    my $f_name = (split/->/,$_)[-1];
    $hash{$id} = $f_name;
}
close $hd;

open $hd, 'log_ca.txt' or die $!;
while(<$hd>) {
    next if /^$/;
    chomp;
    my ($id) = /(\d+)$/;
    my $f_name = (split/->/,$_)[-1];
    $hash2{$id} = $f_name;
}
close $hd;

for (sort keys %hash) {
    print $hash{$_}, "=>", $hash2{$_}, "\n";
}

__END__

Hope this helps.

-- 
J. Peng - QQMail Operation Team
eMail: peng.kyo@gmail.com AIM: JeffHua

Thread Previous


Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About