wusphere has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I have this while loop reading lines in a file. I am adding the offSet value at the end of the forloop, but the value never adds. Any ideas.
my $file = 'test.dat'; my $totalTxns = 0; my $maxSeg = 6; my $segOffset = 24; my $segSize = 240; open(INFILE, $file) or die "Can't read file\n"; while (<INFILE>) { $rectype = substr($_,0,1); if ($rectype eq 'C') { $offSet = $segOffset; for ($seg = 0; $seg < $maxSeg; $seg++) { $txn = substr($_,$offSet,$segSize); $totalTxns++ if($txn =~ /\S/); $offSet += $seqSize; } } }
thanks for all you help

janitored by ybiC: Balanced <code> tags around codeblock to allow easy download of code

Replies are listed 'Best First'.
Re: numbers not adding up!
by pbeckingham (Parson) on Aug 07, 2004 at 23:09 UTC

    Found it - in not using the recommended use strict, you have two variables with very similar names - $segSize and $seqSize. The latter, not being defined, adds nothing to $offset.

    Make all your code start with:

    #! /usr/bin/perl -w use strict; ...
    and this particular problem will never go unnoticed again.



    pbeckingham - typist, perishable vertebrate.
      Thanks very much! I have fixed the problem. You got eagle eyes.

        No, actually, I don't have eagle eyes. I have thirty-nine year old human eyes, and on top of that I'm sitting in a dark room, after an afternoon of driving toward the sun. You completely missed my point! I took your program, added the magic incantations:

        #! /usr/bin/perl -w use strict; ...insert your program here...
        and then I ran it, and Perl itself told me what was wrong with it.



        pbeckingham - typist, perishable vertebrate.
Re: numbers not adding up!
by pbeckingham (Parson) on Aug 07, 2004 at 22:59 UTC

    It does get added to, but on the next line of input, you are resetting the value of $offset to $segOffset. Is that what you are seeing?



    pbeckingham - typist, perishable vertebrate.
      Thanks for your prompt reply. I do reset the offSet value to segOffset when its done within the forloop and then reads the next line. Inside the forloop it should loop for 6 times and add up the offSet value to something like this below: 1st loop = 24 + 240 2nd loop = 244 + 240 3rd loop = 484 + 240 and so on....
        Sorry its should be like. Numbers were incorrect 1st loop = 24 + 240 2nd loop = 284 + 240 3rd loop = 524 + 240 and so on....