CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Win32 Programming Help:
Uninitialized value in hash array, sorting issues.



May 18, 2009, 6:58 AM

Post #1 of 2 (6465 views)
Uninitialized value in hash array, sorting issues. Can't Post


Can anyone lend a hand with my code. Plese help me determine why my code generates

Use of uninitialized value $slice[1] in print at C:/*.pl line 83.

for all iterations up to $slice[7]? The code is supposed to create a sorted directory listing saved to a text doc. Also, my hash array produced 4 duplicates for each instance of a file in the directory. What is causing this? What's wrong with my sort? Thanks in advance.



#!/usr/bin/perl -i_bak

use warnings;

use strict;

use File::stat;

use File::Basename;

use Net::FTP;

use POSIX;

use File::Copy "cp";

#use Win32::CopyFile;

# Hash of arrays



my $subdirs;

my $totalfiles = 0;

my $sumsizefiles;

my $dname = "C:\\Folder\\";

my $localroot = "C:\\Folder.Root";

opendir(INPUT, $dname) or die "Can't open directory $dname: $!";

my $file;

my @fields;

# Set up Console for Windows or UNIX OS

my ($screen_clear, $file_remove, $file_copy, $file_move);

my $OSNAME = $^O;

if ( $OSNAME =~ /MSWin32/)


# Set up Windows variables

$screen_clear = "cls";

$file_remove = "del";

$file_copy = "copy";

$file_move = "move";




# Set up UNIX variables

$screen_clear = "clear";

$file_remove = "rm";

$file_copy = "cp";

$file_move = "rm";



my %content= ();

my (@str_DOW, @str_Month, @int_Day, @str_Time, @int_Year) = ();

my (@TotalArray, @FilesArray, @SizeArray) = ();

# Overwrite dirfiles.txt by creating a new empty file

# Setup OUTPUT to append hash data to the new file in the hash loop

local *OUTPUT;

open(OUTPUT, ">$localroot\\dirfiles.txt") || die "Can't open output file: $!";

open(OUTPUT, "+>>$localroot\\dirfiles.txt") || die "Can't open output file: $!";

while ( $file = readdir(INPUT) )


my $pathtofiles = "$dname\\$file";

subdirs++ and next if -d $pathtofiles;

my $sb = stat($pathtofiles);

$sumsizefiles += $sb->size;


# Split the time string and create array to sort by date

my @time = scalar localtime $sb->mtime;

(@str_DOW, @str_Month, @int_Day, @str_Time, @int_Year) = split /:\s*/, @time, 5;

$TotalArray[$totalfiles] = $totalfiles;

# Construct an hash of each line created by the loop

%content = (Key => $TotalArray[$totalfiles],

Time => $str_Time[$totalfiles],

DOW => $str_DOW[$totalfiles],

Month => $str_Month[$totalfiles],

Day => $int_Day[$totalfiles],

Year => $int_Year[$totalfiles],

File => $FilesArray[$totalfiles],

Size => $SizeArray[$totalfiles],);

my @slice = ();

@slice = @content{"Key","Time","DOW", "Month","Day","Year","File","Size"};

my $newline = 0;

# Simultaneously append (build) the list by printing OUTPUT to file

if ($newline % 8 == 0)


# print OUTPUT "$content{Key}:\t$content{Time}\t$content{Day}\t$content{Month}\t$content{Year}\t$content{DOW}\t$content{File}\t$content{Size}\n";

print OUTPUT @slice;

$newline = 0;




print OUTPUT "\n";



# Make a copy of dirfiles.txt in same directory location

syscopy("$localroot\\dirfiles.txt", "$localroot\\dirfilessorted.txt");

# Open second read from new dirfiles.txt to sort and save to in the

# same directory location

local *INPUT;

open(INPUT, ">$localroot\\dirfilessorted.txt") || die "Can't open output file: $!";

open(INPUT, "+>>$localroot\\dirfilessorted.txt") || die "Can't open output file: $!";

# Sort the hash by number of members and name

foreach $totalfiles (sort { $a cmp $b } keys %content)


print OUTPUT "$content{Key}:\t$content{Time}\t$content{Day}\t$content{Month}\t$content{Year}\t$content{DOW}\t$content{File}\t$content{Size}\n";


# Print unsorted directory content, line by line to STDIO

printf "%s %8.0f %s \n", $file, $sb->size, scalar localtime $sb->mtime;



# Print the total files and sum of file sizes

printf "\n%4d File(s) %d KB\n",



print "Not including $subdirs Sub Directories\n";

close(OUTPUT) || die "Can't close my file: $!";

sleep 10;


Veteran / Moderator

May 18, 2009, 7:13 AM

Post #2 of 2 (6463 views)
Re: [gixxer05] Uninitialized value in hash array, sorting issues. [In reply to] Can't Post

use Data::Dumper;


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives