View Full Version : Tab Delimited Not Returning Values
CTPete
05-06-2008, 04:37 PM
Why is it my code issues are so simple to explain, yet none of the tutorials out there cover them?
I have a text file that is tab delimited. I need to read the file, replace the value of $SEDOL with $CUSIP if $SEDOL is blank (column 4 replaced with column 5 if 4 is blank), and then create a new value for $SHARES that is equal to $SHARES * $IWF (column 13 times column 14)
The code looks correct to my untrained newbie eye, but something is not right..
Any help is greatly appreciated. I'm reading O'Reilly's Learning Perl now..any other suggestions on books/sites to read to get a better grasp of the language would be appreciated too.
Code and file are attached.
FishMonger
05-06-2008, 05:50 PM
You're making some of the same mistakes that were in your other script. Go back to your prior question and read the comments regarding your shebang line and the use strict statement. Once you fix that, the script will generate these warnings/errors.
C:\test>perl -c SPCUSTOM2.pl
Global symbol "$filename" requires explicit package name at SPCUSTOM2.pl line 3.
Global symbol "$writefile" requires explicit package name at SPCUSTOM2.pl line 4.
Global symbol "$filename" requires explicit package name at SPCUSTOM2.pl line 4.
Global symbol "$writefile" requires explicit package name at SPCUSTOM2.pl line 6.
Global symbol "@aColumns" requires explicit package name at SPCUSTOM2.pl line 22.
Global symbol "@aColumns" requires explicit package name at SPCUSTOM2.pl line 23.
Global symbol "@aColumns" requires explicit package name at SPCUSTOM2.pl line 24.
Global symbol "@aColumns" requires explicit package name at SPCUSTOM2.pl line 25.
Global symbol "@aColumns" requires explicit package name at SPCUSTOM2.pl line 26.
Global symbol "@aColumns" requires explicit package name at SPCUSTOM2.pl line 27.
Global symbol "$filename" requires explicit package name at SPCUSTOM2.pl line 46.
Global symbol "$filename" requires explicit package name at SPCUSTOM2.pl line 46.
Bareword "o" not allowed while "strict subs" in use at SPCUSTOM2.pl line 3.
Bareword "pre_" not allowed while "strict subs" in use at SPCUSTOM2.pl line 4.
Bareword "_bak" not allowed while "strict subs" in use at SPCUSTOM2.pl line 46.
SPCUSTOM2.pl had compilation errors.
CTPete
05-06-2008, 07:09 PM
Must have something to do with running it on Windows and then moving it to VMS. If I run the program just like this:
# perl -wuse strict;
$filename = "@ARGV[o]";
$writefile= pre_."$filename";
open (FILE, ">$writefile") || die
my $sLine;
my $cnt = 0;
while ( defined( $sLine = <> ) ) {
$cnt++;
# uncomment if necessary next if $cnt > 50;
chomp $sLine;
my @aColumns = $sLine =~ /(?:^|,)("(?:[^"]+|"")*"|[^,]*)/g;
# Check for bad records
my $fieldcount = scalar @aColumns;
#next if $fieldcount < 62; # low field count
# next if length($aColumns[1]) != 60; # bad cusip
# Define main fieldsmy
my $DATE = $aColumns[0];
my $SEDOL = $aColumns[4];
my $CUSIP = $aColumns[5];
my $SECURITY_NAME = $aColumns[6];
my $SHARES = $aColumns[13];
my $IWF = $aColumns[14];
# Modify Fields
$SEDOL = $CUSIP if $SEDOL == " ";
$SHARES = $SHARES*$IWF;
# Create output file
print FILE join (",",
$DATE,
$SEDOL,
$SECURITY_NAME,
$SHARES,
), "\n";
}
close(FILE);
rename("$filename","$filename"._bak)
I actually get an output file..
If I make the shebang changes, then I get the errors that you mentioned above.
shyam
05-06-2008, 07:31 PM
Must have something to do with running it on Windows and then moving it to VMS. If I run the program just like this:
# perl -wuse strict;
$filename = "@ARGV[o]";
I actually get an output file..
If I make the shebang changes, then I get the errors that you mentioned above.
just because a program runs without compilation errors does not mean that it does what its supposed to do...the first 2 lines of your program has errors because of which they don't run for me...
o is not the same as 0 and array indexes must use numbers
the first 2 lines must actually look like this
#!perl -w
use strict;
my $filename = "$ARGV[0]";
CTPete
05-06-2008, 07:55 PM
Adjusted the ARGV to zero instead of "o" and received an output file but again without the changes that I thought I'd receive from:
$SEDOL = $CUSIP if $SEDOL == " ";
$SHARES = $SHARES*$IWF;
KevinADC
05-06-2008, 09:37 PM
the shebang line problem was well detailed in your other thread. I see no reason to repeat it here again.
FishMonger
05-06-2008, 09:56 PM
First, you need to fix the shebang line. Then fix each of the errors that are produced.
$SEDOL = $CUSIP if $SEDOL == " ";
== is a numeric operator. You need to use eq instead
C:\>perldoc -q quoting
Found in C:\Perl\lib\pods\perlfaq4.pod
What's wrong with always quoting "$vars"?
The problem is that those double-quotes force stringification--coercing
numbers and references into strings--even when you don't want them to be
strings. Think of it this way: double-quote expansion is used to produce
new strings. If you already have a string, why do you need more?
If you get used to writing odd things like these:
print "$var"; # BAD
$new = "$old"; # BAD
somefunc("$var"); # BAD
You'll be in trouble. Those should (in 99.8% of the cases) be the
simpler and more direct:
print $var;
$new = $old;
somefunc($var);
Otherwise, besides slowing you down, you're going to break code when the
thing in the scalar is actually neither a string nor a number, but a
reference:
func(\@array);
sub func {
my $aref = shift;
my $oref = "$aref"; # WRONG
}
You can also get into subtle problems on those few operations in Perl
that actually do care about the difference between a string and a
number, such as the magical "++" autoincrement operator or the syscall()
function.
Stringification also destroys arrays.
@lines = `command`;
print "@lines"; # WRONG - extra blanks
print @lines; # right
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.