At 09:52 12.07.2001 +0100, Govinderjit Dhinsa wrote:
>Does any body know why the printf is not printing anything out!
>
>I tested the rest of the script by replacing the printf line with,
>##########
>print "$line";
>##########
>the file prints (with out any changes to the file, as expected). So
>everything else is working apart from the printf.
>
>Any help would be much appreciated.
>Thanks.
>GD
first of all, add the following line to the top of your script:
use strict;
which will immediately cause everything to stop working :)
However, it will force you to declare your variables in the scope you're
using them in, which is a lot safer and will save you problems down the road.
><script>
>########################################################
This declaration is unnecessary (correct me if I'm wrong);
>&inserter;
sticking my in front of variable declarations to make strict happy:
>sub inserter {
>open (TESTFILE, "<CALL_SUMMARY_CHANGES4_5.HADES;21");
you should check the result of open and get out if your file doesn't open
open (TESTFILE, "<CALL_SUMMARY_CHANGES4_5.HADES;21") || die("Couldn't open
file $!"); # $! holds the OS's error
>$file = <TESTFILE>;
>$length = length($file);
>@array = split(//, $file);
>$splitcounter = 0;
becomes:
my $file = <TESTFILE>;
my $length = length($file);
so that strict doesn't complain
this is kind of a slow way to split your string up.
>@array = split(//, $file);
>$splitcounter = 0;
>
>for ($counter = 0; $counter < $length; $counter++) {
> if ($splitcounter == 480) {;
> push(@newarray, "\r");
> $splitcounter = 0;
> $counter--;
> }
> else {
> push(@newarray, $array[$counter]);
> $splitcounter++;
> }
> }
>}
Maybe try something like this?
my @new_array; # declare this before using it
while($length > 480)
{
my $short_string = substr($file, 0, 480);
$file = substr($file, 480);
$short_string .= "\r";
push(@new_array, $short_string);
$length = length($file);
}
push(@new_array, $file); # grab the last bit
>foreach $line(@newarray) {
>
> $a = substr($_, 0, 18);
> $b = substr($_, 32, 1);
> $c = substr($_, 290, 10);
> printf "%18s %1s %10s\n",$a, $b, $c;
>}
>########################################################
you don't need $line if you're happy using $_, so:
foreach (@new_array)
{
# don't forget to 'my' these
my $a = substr($_, 0, 18);
my $b = substr($_, 32, 1);
my $c = substr($_, 290, 10);
printf "%18s %1s %10s\n",$a, $b, $c;
}
your problem may have been due to @newarray not getting filled the way you
wanted or something, but this is a cleaner way to split up your lines, anyway
Aaron Craig
Programming
iSoftitler.com