Help - Search - Member List - Calendar
Full Version: Hash headaches
WorkTheWeb Forums > Webmaster Resources > Perl Beginner Help
Support our Sponsors!
Terry Poperszky
I am working on a bit of logic to be used in a script that copies new files
to a temporary directory and I am stumped. Basically what I want to
accomplish is to be able to look for the existence of a file name in a log
file, and I am trying to use a hash to accomplish this.



$directory is the listing of the current files in the directory

$dir_log currently is file containing the same information as $directory.



I am trying to see if the elements of $directory exist as keys in the hash,
but it isn't working.



Suggestions/Observations?





Terry Poperszky

Voice

Fax

[Email Removed]







open MYFILE, "<dir_log";



while (<MYFILE>) {

$dir_log{chomp($_)} = 1;

}



chomp ($directory = `dir /b /O-D`);



foreach ($directory) {

print "success" if (exists $dir_log{$directory});

}

Jenda Krynicky
From: "Terry Poperszky" <[Email Removed]>
QUOTE
I am working on a bit of logic to be used in a script that copies new
files to a temporary directory and I am stumped.  Basically what I
want to accomplish is to be able to look for the existence of a file
name in a log file, and I am trying to use a hash to accomplish this.

$directory is the listing of the current files in the directory

$dir_log currently is file containing the same information as
$directory.

I am trying to see if the elements of $directory exist as keys in the
hash, but it isn't working.

open MYFILE, "<dir_log";



while (<MYFILE>) {

$dir_log{chomp($_)} = 1;

chomp() doesn't return what you seem to think. You have to change the
line above to
chomp(); $dir_log{$_} = 1;

Jenda
===== [Email Removed] === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery

Charles K. Clarkson
Terry Poperszky <[Email Removed]> wrote:

: Suggestions/Observations?

Don't use external commands to get directory contents.
Use 'openddir' and 'readdir' instead.


: open MYFILE, "<dir_log";

You should always check that I/O succeed.


: while (<MYFILE>) {
: $dir_log{chomp($_)} = 1;

'chomp' will not return a file name here. I think you
want this.

chomp;
$dir_log{$_} = 1

: }


HTH,

Charles K. Clarkson
--
Mobile Homes Specialist


Terry Poperszky
Charles and Jenda, thanks for your responses. In addition to the change that
you both suggested, I had a problem with the array variable @directory in my
exists test. I was testing the entire array instead of just the current
cell. Charles, thanks for your suggestion on opendir, I will look into it.





Terry Poperszky









chomp (@directory = `dir /b /O-D`);



open MYFILE, "<dir_log" or die "Log File Missing";



while (<MYFILE>) {

chomp (); $dir_log{$_} = 1;

}



foreach (@directory) {

print $_ if (exists $dir_log{$_});

}


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2005 Invision Power Services, Inc.