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

Is it possible to get this file count ($ct) to give me the total count after it does the counter in the for loop on the top of a page?? All my scripts I have to put the total count after the for loop at the bottom of page but was wondering if I can "call" the total $ct and put it at the top of the page: Here is my current output:
Total Count = 0 #I want the total count of 5 here. . .. file1 file2 file3 Total count = 5 #I want this total count up on the top of output inst +ead of here.
The script:
use strict; my $newfile = "./"; opendir(DIR,$newfile); my @files = readdir(DIR); close(DIR); my $ct = 0; chdir ($newfile) || die "Could not change to $newfile directory\n$!\n" +; print "Total count = $ct\n\n"; for(@files) { print "$_\n"; $ct++ } print "\nTotal count = $ct\n";

Replies are listed 'Best First'.
Re: Need counter assistance
by RMGir (Prior) on Aug 30, 2002 at 12:58 UTC
    Sure, since you know the number of files before you start.
    print "Total count = ",(scalar @files),"\n\n"; for(@files) { print "$_\n"; $ct++ } # leaving this in here so you can make sure it prints the # same thing print "\nTotal count = $ct\n";
    "(scalar @files)" means "the number of items in the @files array", which is, of course, your count.
    --
    Mike
      Thanks to all of you but I am still have quesions about this:
      print "Total count = ",(scalar @files),"\n\n";
      What and how did you come up with this?
      (scalar @files) is doing what with the files array?? How does it work in getting the count as you did? I assume scalar is some sort or function.
        Perl has a concept called "context". Different functions or constructs return different things depending on whether they're called in a list (or array) context or a scalar context.

        For example, you could have just said

        my $ct=@files;
        and $ct would have been set correctly, because that assignment is a scalar context, and in a scalar context an array or list returns its number of elements.

        Since "print" is a list context, I had to use the "scalar" operator to get the count of elements rather than the list of values.

        Check out "perldoc -f scalar" and "perldoc perldata" for more information.
        --
        Mike

        scalar is a builtin function that forces an expression to be interpreted as a scalar (i.e. return a single value). When you force an array to become a scalar it returns its length (note that length starts from 1 so the number it returns is not going to be the last element of your array).

        Don't worry though, using scalar does not modify your array, you can use it again later and it will still have all it's values in place.

        Chris

        Lobster Aliens Are attacking the world!
Re: Need counter assistance
by simeon2000 (Monk) on Aug 30, 2002 at 12:53 UTC
    Code after the CHDIR like so:

    # snip until the chdir print "Total count = ", scalar(@files), "\n\n"; print "$_\n" for @files;
    Arrays called in scalar context yield their length. Remember this, Monk::Anonymous ;) It'll save you lots of coding.

    --
    perl -e "print qq/just another perl hacker who doesn't grok japh\n/"
    simeon2000|http://holdren.net/