AndrewMcHorney wrote:
Hi

Hello,

I appreciate the assistance from a previous email message on writing functions in Perl. The issue right now is that I am passing an array, a number and a string to the function but it does not appear to be passed successfully. I am sure the error is quite simple. The function in question is build_string. Any assistance would be greatly appreciated.



#! /bin/perl

use warnings;
use strict;

sub array_eq
{
  my $aref = shift;
  my $bref = shift;
  if (@$aref != @$bref)
  {
     return 0
  }
  # compare size
  for my $i (0..$#$aref)
  {
     #
     # compare values
     #
     return 0 unless $$aref[$i] eq $$bref[$i];
  }
  return 1;
}

sub build_string
{

  my $array = shift;

  my $start_index = shift;

  my $separator = shift;

You do not use this variable anywhere in this subroutine.

  $array_size = @$array;

  $built_string = "";
print "passed in array".$array[$start_index]."\n";

$array contains a reference to an array so you have to dereference it to access an element:

  print "passed in array" . $array->[ $start_index ] . "\n";

  while ($start_index < $array_size +1)

You are trying to access an element of the array past the end of the array.

  {
     $built_string = $built_string.$array[$start_index];

     $start_index = $start_index + 1;
  }

  return $built_string;
}

That subroutine could be simply written as:

sub build_string {
    my ( $array, $start_index ) = @_;
    return join '', @{ $array }[ $start_index .. $#$array ];
}

#
# Read the list of drives to go through
#

$num_args = @ARGV;

$DRIVE_LIST_FILE = "DriveList.txt";

open DRIVE_LIST_FILE, $DRIVE_LIST_FILE or die "Cannot open drive list file\n";

You should include the $! or $^E variable in the error message so you know *why* open failed and probably also the file name so you know which file failed to open.

@DriveList = (<DRIVE_LIST_FILE>);

The parentheses are superfluous.

$DriveListSize = scalar(@DriveList);

The use of scalar() is superfluous.

close DRIVE_LIST_FILE;

$DriveIndex = 0;
$FileCount = 0;
$DirectoryCount = 0;

#
# Create the duplicate file list file
#

($Seconds,$Minutes,$Hours,$Day,$Month,$Year) = (localtime)[0,1,2,3,4,5];
$Year = $Year + 1900;

Or simply:

 my ( $Seconds, $Minutes, $Hours, $Day, $Month, $Year ) = localtime;

$Date = sprintf("%02d",$Month)."-";
$Date = $Date.sprintf("%02d",$Day)."-";
$Date = $Date.sprintf("%04d",$Year)."-";
$Date = $Date.sprintf("%02d",$Hours)."_";
$Date = $Date.sprintf("%02d",$Minutes)."_";
$Date = $Date.sprintf("%02d",$Seconds);

Or simply:

my $Date = sprintf '%02d-%02d-%04d-%02d_%02d_%02d', $Month + 1, $Day, $Year + 1900, $Hours, $Minutes, $Seconds;

$DuplicateFileName = "Duplicate_File_List_".$Date;
#print $DuplicateFileName."\n";

$OpenStatus = 1;
open DUPLICATE_FILE_NAME, ">".$DuplicateFileName or $OpenStatus = 0;
if ($OpenStatus == 0)
{
   print "$!";
   print " ";
   die "Unable to open duplicate file name file\n";

The output from print() is normally buffered and the output from die() is unbuffered so the third line will display immediately and the first two lines will display when the buffer is full or flushed.

}
#
# Create the open failed file list file
#
$OpenFailedFileName = "Open_Failed_File_List_".$Date;
#print $OpenFailedFileName."\n";

open OPEN_FAILED_FILE_NAME, ">".$OpenFailedFileName or die "Unable to open open 
failed file name file\n";

You should include the $! or $^E variable in the error message so you know *why* open failed and probably also the file name so you know which file failed to open.

#
# Loop through each drive to get a list of files
#
while ($DriveIndex < $DriveListSize)
{
    $CurrentDrive = @DriveList[$DriveIndex];

That should be:

    $CurrentDrive = $DriveList[ $DriveIndex ];


[ *SNIP* ]



John
--
Those people who think they know everything are a great
annoyance to those of us who do.        -- Isaac Asimov

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to