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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



gixxer05
Novice

May 18, 2009, 6:58 AM

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

Greetings,

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.

JM




Code
 

#!/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

while(1)

{

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";

}

else

{

# Set up UNIX variables

$screen_clear = "clear";

$file_remove = "rm";

$file_copy = "cp";

$file_move = "rm";

}

system("$screen_clear");

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;

$totalfiles++;

# 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;

}

else

{

print OUTPUT "\n";

$newline++;



}

# 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;

}

closedir(INPUT);

# Print the total files and sum of file sizes

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

$totalfiles,

$sumsizefiles/1024;

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

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

sleep 10;



}




FishMonger
Veteran / Moderator

May 18, 2009, 7:13 AM

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

use Data::Dumper;
http://search.cpan.org/~ilyam/Data-Dumper-2.121/Dumper.pm

 
 


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

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