marctwo has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Passing Array of Arrays
by arturo (Vicar) on Mar 11, 2003 at 13:07 UTC | |
|
Re: Passing Array of Arrays
by broquaint (Abbot) on Mar 11, 2003 at 12:53 UTC | |
|
Re: Passing Array of Arrays
by robartes (Priest) on Mar 11, 2003 at 13:07 UTC | |
by marctwo (Acolyte) on Mar 11, 2003 at 14:53 UTC | |
|
Re: Passing Array of Arrays
by Tanalis (Curate) on Mar 11, 2003 at 12:56 UTC | |
by broquaint (Abbot) on Mar 11, 2003 at 13:15 UTC | |
by Tanalis (Curate) on Mar 11, 2003 at 13:26 UTC | |
by broquaint (Abbot) on Mar 11, 2003 at 13:57 UTC | |
by zigdon (Deacon) on Mar 11, 2003 at 13:53 UTC | |
|
Re: Passing Array of Arrays
by nite_man (Deacon) on Mar 11, 2003 at 13:30 UTC | |
|
Re: Passing Array of Arrays
by SheridanCat (Pilgrim) on Mar 11, 2003 at 22:15 UTC |