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

Hi All,
I'm editing a script that has the following:
my @missing; foreach my $f (@fields) { if ($account{$f} eq undef) { push(@missing, @acct_fields{$f}); } }

I understand what this is doing, but the following part that throws me off...although i can assume what it's doing i want to know how it actually works:
if ($#missing > -1) { $" = ', '; do something.... }

Thanks in advance for your help.
-Kiko

Replies are listed 'Best First'.
Re: What does $# stand for?
by tstock (Curate) on Oct 15, 2001 at 00:00 UTC
    $#array returns the last index in a array. So, one less than the number of elements in a array.
    The code above is pretty ugly, and I would replace it by
    if (scalar @missing)

    other examples:
    scalar @array == ($#array + 1) # is always true unless you redefine where the index starts. for my $i (0..$#array) { # loops through the array by index number }

      Actually the scalar in the first case isn't necessary since boolean context is (more or less) scalar context as is shown with the code below.

      @a = (); print "try 1\n"; print "Has elements\n" if @a; push @a, 1; print "try 2\n"; print "Has elements\n" if @a;
Re: What does $# stand for?
by mischief (Hermit) on Oct 15, 2001 at 19:31 UTC

    You might also want to look at perlvar if you come across any special variables you don't recognise in the future.

Re: What does $# stand for?
by gothic_mallard (Pilgrim) on Oct 15, 2001 at 21:02 UTC
    I guess it might be worth noting a little caveat here that has caught innumerous novices in their first brushes with perl.

    Many people tend to forget in the heat of the moment that $# gives you the last index of the list - not the number of elements in it.
    Because of this: $#list won't give you the same result as scalar @list.
    This, I will admit, caught me a fair few times when I was starting out.

    Just a simple thought, probably beneath most of you guys out there, but one worth remembering ;)