in reply to Need counter assistance

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

Replies are listed 'Best First'.
Re: Re: Need counter assistance
by Anonymous Monk on Aug 30, 2002 at 13:47 UTC
    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

        Thanks for your answers and quick responses!

      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!