Hi Experts,
I am learner in perl, i have question here please help me out
i have some files which contains data like below:
input
file 1
STD Area Name Amount
2 1 AB 1.0
2 2 AB-1 2.0
4 1 AB-4 1.0
4 2 AB-6 2.0
file 2
STD Area Name Amount
21 01 AB 4.0
22 12 AB-1 6.0
41 44 AB-4 9.0
42 46 AB-6 4.0
42 46 AB-6 4.0
file 3
STD Area Name Amount
01 10 AB 4.0
20 02 AB-1 6.0
40 04 AB-4 9.0
02 26 AB-6 4.0
file 4
STD Area Name Amount
56 01 AB 4.0
56 12 AB-1 6.0
65 44 AB-4 9.0
65 46 AB-6 4.0
Output:
file 1 file 2 file 3 file 4
AB 1.0 4.0 4.0 4.0
AB-1 2.0 6.0 6.0 6.0
AB-4 1.0 9.0 9.0 9.0
AB-6 2.0 8.0 4.0 4.0
Each file contains common name where i need to add the Amount. can you please let me know how can i do this.
i tired as mentioned below:
my %result;
my %hash = ( '2.1','AB',
'2.2','AB-1',
'4.1','AB-4',
'4.2','AB-6',
'21.01','AB',
'22.12','AB-1',
'41.44','AB-4',
'42.46','AB-6',
'42.46','AB-6',
'01.10','AB',
'20.02','AB-1',
'40.04','AB-4',
'02.26','AB-6',
'56.01','AB',
'56.12','AB-1',
'65.44','AB-4',
'65.46','AB-6');
foreach my $file (@files) {
open FILE, "<$dir/$file" or warn "Couldn't open file ($!) or file ($!)
+ not found\n";
while (chomp ( my @fields = split /\t/, <FILE>) ) {
my $key = "$fields[0].$fields[1]";
if (exists $hash{$key})
{
$key = $hash{$key};
if (exists($result{$key}))
{
$result{$key} += $fields[03];
}
else {
$result{$key} += $fields[03];
}
}
}
}
close FILE;
-
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.