in reply to How to determine if an array passed as an argument to a subroutine is zero-length

w/o seeing some code its tough to know exactly what you are asking.... Here is one attempt to answer your question.
#!/usr/bin/perl -wT use strict; my @arr = qw(a b c d e); # make a non-empty array my @arr2 = qw(); # make an empty array my $val = isempty(\@arr); # check if arr isempty my $val2 = isempty(\@arr2); # check if arr2 isempty print "\@arr ", $val ? 'is' : 'is not', " empty.\n"; print "\@arr2 ", $val2 ? 'is' : 'is not', " empty.\n"; sub isempty { my $aref = shift; # get array ref as param return !@$aref; # check array for emptiness } =output @arr is not empty. @arr2 is empty.

-Blake

  • Comment on Re: How to determine if an array passed as an argument to a subroutine is zero-length
  • Download Code

Replies are listed 'Best First'.
Re: Re: How to determine if an array passed as an argument to a subroutine is zero-length
by Withnail (Novice) on Sep 07, 2001 at 21:06 UTC
    Thanks for your input, people!

    Sorry about the lack of code example before. Here's what I've come up with so far:

    get_highest_vers(\@vers); sub get_highest_vers { my $vers_index; my @sorted_vers; my @locvers = @_; $#sorted_vers = -1; if ($#locvers == -1) { $highest_vers = 0.00; } else { for ($vers_index = 0; $vers_index <= $#locvers; $vers_index++) { $locvers[$vers_index] =~ s/\.\/v//; $locvers[$vers_index] += 0; } # Sort the archive version numbers and grab the highest one @sorted_vers = sort {$b <=> $a} @locvers; $highest_vers = shift(@sorted_vers); } ($major, $minor) = split(/\./, $highest_vers); }

    This seems to work, (although I can't help feeling it's a little ornate!)

    Anyway, I'm still having problems when I try and move the subroutine to a seperate module and call it in using the "use Module" construct:

    I get a complaint that the argument given in the subroutine call should be an array, not a reference constructor. It also says that no implicit dereferencing is allowed--use the {EXPR} forms as an explicit dereference.

    Can anyone advise? BTW, I'm using "diagnostics" and "strict".

    -Withnail