Quoth "tbb!/fbr! said:
Here's a piece of code which compares lines in one file against lines in
the other. I mentioned earlier in the thread it was something I was
working on, but can't quit get it right via hashes, splits, working on
the data:
You haven't explained what isn't working. AFAICS the code below should
do *something*; I can't tell whether or not it what you want it to do.
foreach $line1 ( @lines1 )
foreach my $line1 ( @lines1 )
Are you using 'strict'?
You should give your variables more meaningful names than '@lines1'.
What does this file actually contain?
[I've rewrapped the code below since the lines were too long; since the
wraps were inside strings it no longer does what it did. Please wrap any
posted code to 76 columns: if necessary you can divide a long string up
with .]
{
( $symb, $company, $excess ) = split( /\t/, $line1, 3 );
foreach $line2 ( @lines2 ) #
{
( $date_y, $symbol_y, $company_y, $cap_y, $open_y, $low_y,
$high_y, $close_y, $pe_ratio_y, $date_div_y, $dividend_y, $div_yield_y,
$date_ex_div_y, $nav_y, $yield_y, $vol_y, $avg_vol_y ) = split(/\t/,
$line2 );
if ( $symb eq $symbol_y )
{
print "$symb\t$company\t$cap_y\t$open_y\t$low_y\t
$high_y\t$close_y\t$vol_y\t$avg_vol_y\t$excess\n";
print FILEOUT1 "$symb\t$company\t$cap_y\t$open_y\t
Don't use global bareword filehandles. Keep your filehandles in
variables: that is, instead of
open FILEOUT, ">", ... or die ...;
write
open my $FILEOUT, ">", ... or die ...;
and then use
print $FILEOUT ...
later on. Also, use a meaningful name for your filehandle variables.
$low_y\t$high_y\t$close_y\t$vol_y\t$avg_vol_y\t$excess\n";
You can make this a lot less messy by noticing that $cap_y--$close_y and
$vol_y--$avg_vol_y stay together, so you can treat them as one field.
Also, you are printing the same string twice, so put it in a variable
rather than writing it out all over again.
# create a pattern to match a single field, and a pattern to match a
# field plus a delimiter
my $f = qr/[^\t]*/;
my $fd = qr/$f\t/;
# extract the fields we want
my ($symbol, $data, $vols) = $line2 =~ m{
^ $fd ($f) \t $fd ($fd{4} $f) \t $fd{7} ($fd $f) $
}x;
# strip the trailing tab from the field values
s/\t$// for $symbol, $data;
if ($symbol eq $symb) {
my $new = join "\t",
$symb, $company, $data, $vols, $excess;
print $new;
print FILEOUT $new;
}
An alternative would be something like this:
# outside the loop
my @in = qw/
date symb company
cap open low high close pe_ratio
date_div div div_yield date_ex_div
nav yield vol avg_vol
/;
my @out = qw/
cap open low high close vol avg_vol
/;
# inside
my %f;
@f{@in} = split /\t/, $line1;
if ($f{symb} eq $symb) {
my $new = join "\t",
$symb, $company, @f{@out}, $excess;
# print as above
}
which is likely to be more use if you want to do more processing of this
data later.
Ben