in reply to Re: Need counter assistance
in thread Need counter assistance

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.

Replies are listed 'Best First'.
Re: Re: Re: Need counter assistance
by RMGir (Prior) on Aug 30, 2002 at 13:55 UTC
    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!
Re: Re: Re: Need counter assistance
by cfreak (Chaplain) on Aug 30, 2002 at 13:57 UTC

    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!