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

contents of file(file.txt) to read.Read it vertically

apple snake cow dog

#!/usr/bin/perl -w use strict; open(FH,'<','file.txt') or die "File is not there $!\n"; my $count=1; while(<FH>) { print $count++; print ":$_"; }

output: 1:apple 2:snake 3:cow 4:dog

Output is as expected but my question is before apple why is it printing 1.I initialized count to 1 and entered the while loop and before printing content(at least the first one) there is count++ which it has to make it start from 2.I would like to know where i am making wrong assumption.Thanks

Replies are listed 'Best First'.
Re: Question on Reading a file inside while and counting no.of lines
by golux (Chaplain) on Jun 15, 2013 at 21:16 UTC
    vyeddula,

    There's a difference between $count++ and ++$count.

    The first one increments it after using it (ie. prints its value first, and then adds one). The second increments it before using it.

    Try using ++$count instead, and you should see the answer you expect.

    Look at the first example in "Auto-increment and Auto-decrement" here.

    say  substr+lc crypt(qw $i3 SI$),4,5
Re: Question on Reading a file inside while and counting no.of lines
by davido (Cardinal) on Jun 15, 2013 at 21:57 UTC

    So your immediate problem would be solved by preincrementing instead of postincrementing, so that the incrementing happens before you print.

    But your code could be simplified further by using $., documented in perlvar.

    while( <FH> ) { print "$.:$_"; }

    It starts at zero, so if you need it to start counting at some other base you'll have to add.

    print $. + 1, ":$_";

    Dave

      It starts at zero…

      Actually, it starts at 1, not 0.

      C:\strawberry>perl -nE "say $." README.txt | head 1 2 3 4 5 6 7 8 9 10 C:\strawberry>

      It's probably also worth demonstrating here the usual idiom for resetting the line number to 1 for each file in @ARGV when using <>.

      # @ARGV may have more than one file in it while (<ARGV>) { print "$.:$_\n"; } continue { close ARGV if eof; }

        *head-bonk*

        Ever since the alien abduction I've had a hard time remembering that $. is 1-based. ;)


        Dave

        It starts at zero...

        Quoth perlvar (emphases added): Each filehandle in Perl counts the number of lines that have been read from it. ... When a line is read from a filehandle (via "readline()" or "<>") ... $. becomes an alias to the line counter for that filehandle.

        So I would say davido's assertion is not without justification:  $. starts out undefined (which is very like zero) and happily becomes 1 by aliasing when the first line is read. I assume the internal filehandle line counter is 0 or undefined prior to any read on the handle. So there.

        >perl -wMstrict -le "my $filename = 'text'; open my $fh, '<', $filename or die qq{opening '$filename': $!}; ;; print qq{\$. initially: }, defined $. ? qq{'$.'} : 'undefined'; ;; while (<$fh>) { chomp; print qq{$.: '$_'}; } ;; close $fh or die qq{closing '$filename': $!}; " $. initially: undefined 1: 'now is the time' 2: 'foo bar baz' 3: 'how now brown cow' 4: 'four score and seven'
Re: Question on Reading a file inside while and counting no.of lines
by 2teez (Vicar) on Jun 15, 2013 at 21:19 UTC

    Hi vyeddula,
    I initialized count to 1
    Yes you did.

    there is $count++ which it has to make it start from 2

    No, with print $count++; you are saying, print the present value of $count, then increase it value by 1.

    So if you want the value to start from 2, then increase the value first then print like so: print ++$count it is called pre-incremental.

    Test this:

    my $count = 1; while(<DATA>) { print ++$count; print ":$_"; } __DATA__ apple snake cow dog

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: Question on Reading a file inside while and counting no.of lines
by Laurent_R (Canon) on Jun 15, 2013 at 22:10 UTC

    You have been given three good answers, but I especially support Davido's recommendation to use the "$." variable (line count in the file being read) rather than counting yourself the line number. It is simpler, easier and probably faster.

Re: Question on Reading a file inside while and counting no.of lines
by ww (Archbishop) on Jun 16, 2013 at 11:57 UTC
    You ask us to "(r)ead it vertically.

    We could be expected to do that pretty much automatically if you took the trouble to format your nodes as recommended by the directions at the text-input box where you created this.

    Use code tags ( <c>...</c> ) around data, as well as around code.


    If you didn't program your executable by toggling in binary, it wasn't really programming!