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

My current project requires that I archive data releases in a series of numbered sub-directories. The directories are, rather unimaginatively, called v1.00, v2.00, v2.01, for example. My routine will create the next version sub-directory, e.g. v3.00, before archiving the new release of data there.

Here's the question: I'm using Find::File::name to get the names of pre-exisiting "vX.YY" directories. I push the output of Find::File::name onto an array. How do I pass the array, (array reference), to a subroutine and determine if it is zero-length or not?

What I've written so far seems to work provided the subroutine is in the same file as the main program. What I want to do is place the sub in a module and this is where it all goes wrong!

Any ideas?

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

Replies are listed 'Best First'.
Re: How to determine if an array passed as an argument to a subroutine is zero-length
by blakem (Monsignor) on Sep 07, 2001 at 04:28 UTC
    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

      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

Re: How to determine if an array passed as an argument to a subroutine is zero-length
by Zaxo (Archbishop) on Sep 07, 2001 at 04:32 UTC

    # array passed as list if (@_) { # do non-empty things } else { # do empty things }
    In a conditional, @_ is in scalar context and returns its length.

    After Compline,
    Zaxo