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

Hi,

I have a perl script that passes an array into a sub routine ( &viewThumbnails(@files) ) The array has the following elements in the showAllFiles() subroutine...

(output from browser $cgi output)

./pics ./pics/0001-20021005-005-002-002-001013007-Me_in_some_snow_somewhere.j +pg ./pics/0003-20021101-004-003-002-001-Me_ontop_of_flat_top_mountain.jpg ./pics/0000-20020918-001-005-003-013011005-Me_in_a_bush.jpg

However once I have passed @files into "&viewThumbnails(@files)" the array only contains...

(output from browser $cgi output)

./pics

What am I missing here? The first element of the array has been passed to the subroutine but nothing else? Thats crazy!

Here's a few code snipets to help...

sub showAllFiles{ &printHeader("Displaying All Files"); @files = (); find(sub{push @files, $File::Find::name}, $picPath); foreach $file (@files){ print $cgi->p("$file"); } &viewThumbnails(@files); } $numCols = 4; sub viewThumbnails{ @filesToDisplay = @_[0]; foreach $file (@filesToDisplay){ print $cgi->p("$file"); } $count = 0; print "\n<table>\n"; print " <tr>\n"; foreach $file (@filesToDisplay){ if ($count = $numCols){ $count = 0; print " <\/tr>\n"; print " <tr>\n"; print " <td><img src=\"$file\"><\/td>\n"; }else{ $count++; print " <td><img src=\"$file\"><\/td>\n"; } } print " <\/tr>\n"; print "<\/table>\n"; }

The two subroutines are in different perl scripts but are being related by...

use lib('.'); require 'viewThumbnails.pl';

But I don't think this is breaking it. Just though I should mention it incase I'm missing something obvious.

Thanks for reading this far!

Any help is much appreciated

M

Replies are listed 'Best First'.
Re: Missing array values after sub routine?
by Mr. Muskrat (Canon) on Nov 15, 2002 at 23:35 UTC

    Here is what you have:

    sub viewThumbnails{ @filesToDisplay = @_[0];

    Here is what it should be:

    sub viewThumbnails{ @filesToDisplay = @_;

    Had you enabled warnings with -w or use warnings; it would have flagged that line.

      Thank you! - I will start to use warnings I promise!
Re: Missing array values after sub routine?
by Zaxo (Archbishop) on Nov 15, 2002 at 23:36 UTC
    The line      @filesToDisplay = @_[0]; should be      @filesToDisplay = @_;

    After Compline,
    Zaxo

      Zaxo & Mr. Muskrat you probably don't care about my extra few saint hood points I'll send your way because of this incredinly quick response but they are on there way neverless

      Thanks all

      Mark

      (The man who will start using strict & warnings)
Re: Missing array values after sub routine?
by Enlil (Parson) on Nov 15, 2002 at 23:39 UTC
    I believe the problem lies in:
    @filesToDisplay = @_[0];
    As all you are passing to @filesToDisplay is the first element in @_ which is just a flattened list of passed parameter @files. What you are probably looking for is:
    my @filestoDisplay = @_;

    -enlil

Re: Missing array values after sub routine?
by janjan (Beadle) on Nov 16, 2002 at 01:10 UTC
    I echo what others have said about @_, but wanted to add a mention of using array references. Arrayrefs probably aren't necessary for this particular application, but in larger settings, or if you want to pass multiple arrays or hashes, you could do something like this:
    &viewThumbnails(\@array1, \@array2); # and then your subroutine looks like: sub viewThumbnails { my ($firstset, $secondset) = @_; # do something with these arrayrefs }
    Also, TMTOWTDI, but I almost never refer to elements of @_ by array index - I find it's easier to read something like
    my ($zero, $one, $two) = @_;
    than something like:
    my $zero = @_[0]; my $one = @_[1]; my $two = @_[2];
    Also, if I know I only want the first element of @_, I tend to use shift, like so:
    my $firstarg = shift;
    ...but only because I find it easier to read than explicitly using @_ for just one value. (Apologies if this is way more info than you were looking for; I'm just brainstorming a bit after a long day of looking at others' code.)
Re: Missing array values after sub routine?
by robartes (Priest) on Nov 15, 2002 at 23:39 UTC
    In viewThumbnails, change this line:
    @filesToDisplay = @_[0];
    to
    @filesToDisplay= @_;
    You were only using the first element of the array passed, which happened to be /pics.

    CU
    Robartes-