Please help me! I am just starting out in Perl and am totally confused by what is happening. I am trying to build an array of arrays in one function, pass it out in to the main scope of the script, then back into another function to print it's contents.

The array of arrays is filled correctly but when I come to print it it apparently contains the value of the last element repeated throughout the array!

Please tell me what I am doing wrong (apart from everything!

@datalist = GetLogFilesAndDatesIntoArray($logdirectory); ExtractData(@datalist); sub GetLogFilesAndDatesIntoArray { my @filenames; my @filesanddates; my @fileanddate; my @sortedfilesanddates; my $dev, my $ino, my $mode, my $nlink, my $uid, my $gid, my $rdev, + my $size, my $atime, my $mtime, my $ctime, my $blksize, my $blocks; my $filecounter; my $filename; my $logdirectory = $_[0]; #Get passed log direc +tory name chdir($logdirectory) || die "Cannot open " . $logdirectory; #Ch +ange to log file directory @filenames = <post*.*>; #Get list of UP log + files in directory $filecounter = 0; foreach $filename (@filenames) { #Get file info on every file and store name and date in array print "Checking ".$filename."\n"; ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, +$mtime, $ctime, $blksize, $blocks) = stat $filename; @fileanddate = ($filename, $mtime); $filesanddates[$filecounter] = \@fileanddate; print $filesanddates[$filecounter][0]."\n"; $filecounter++; } #Sort by date #@sortedfilesanddates = sort { $a->[1] <=> $b->[1] } @filesanddate +s; return(@filesanddates); #Return array conta +ining the data } sub ExtractData { my @filesanddates = @_; #Get the array pass +ed into the function my $logfilename; my $filedate; my $status = "Success"; my @errormessage; my $errorline = 0; my $message; my $service; my $comment; my $firstline = "true"; my $listcount; my @header; my $theline; chdir($logdirectory) || die "Cannot open " . $logdirectory; #Ch +ange to log file directory open(REPORT, ">" . $report) || die "Cannot open ".$report; #Ope +n the report file print "Checking the list\n Length is ".$#filesanddates."\n"; #then process all other logs for that day for($listcount = 0; $listcount < $#filesanddates; $listcount++) { $logfilename = $filesanddates[$listcount][0]; open(LOGFILE, $logfilename) || die "Cannot open ".$logfilename +; #*** THE LOGFILENAME IS THE LAST ELEMENT OF THE ORIGINAL ARRAY R +EPEATED *** print "Searching ".$logfilename." for errors\n"; print "listcount: ".$listcount."\n\n"; .....etc

In reply to Passing Array of Arrays by marctwo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.