Need help with table

O

Orion93

Hi!

I use File::Find and actually my script edit all files in a treefiles but
don't exclude the files with extension define in extension.def.

The line "elsif ($tabext{$ficext} != undef) {}" doesn't work, I am not able
to say it to compare if the extension of the file find is inclue in
extension.def to exclude it.

Thanks...

Sophie

Sorry for my english...

Script:
-------

#!/usr/bin/perl -w

use strict;
use warnings;
use diagnostics;
use File::Find;

sub listePages {
my $ext1="c:\\result.txt";
my $direct="c:\\";
open (Desc, "< extension.def");
my %tabext;
while (<Desc>){
$tabext{$_}='1';
}

find(\&accounts, $direct);

sub accounts {
$File::Find::name =~ /(\..*)$/;
my $ficext = $1;
#Pour eliminer les dossiers
if (-d $File::Find::name) {}
#Pour eliminer les fichiers ayant une extension étant dans la liste
elsif ($tabext{$ficext} != undef) {}

else {
open SORTIE,">> $ext1";
print "$File::Find::name \n";
print SORTIE "$File::Find::name \n";
close SORTIE;
}
close Desc;
}
}


File extension.def
---------------------
..zip
..doc
..rtf
..xls
..etc
 
S

Sherm Pendley

Orion93 said:
while (<Desc>){
$tabext{$_}='1';
}

When you read a text file like this, the entire line, *including* the
newline character(s) at the end, is read into $_. So the keys in %tabext
are not what you're expecting.

What you need to do here is get rid of the trailing newlines with chomp():

while(<Desc>) {
chomp;
$tabext{$_}='1';
}
Sorry for my english...

Don't be - it's better than that of many "native" speakers. Besides, I
failed (twice) in my attempt to learn French, so I'm in no position to
criticize. ;-)

sherm--
 
T

Tad McClellan

Orion93 said:
I use File::Find and actually my script edit all files in a treefiles but
don't exclude the files with extension define in extension.def.

The line "elsif ($tabext{$ficext} != undef) {}" doesn't work, I am not able
to say it to compare if the extension of the file find is inclue in
extension.def to exclude it.

use strict;
use warnings;


Thank you for your courtesy. We appreciate your respect for other's time.

sub listePages {
my $ext1="c:\\result.txt";


It is easier to read and understand if:

my $ext1 = 'c:\result.txt'; # no doubled backslashes
or
my $ext1 = 'c:/result.txt'; # no backslashes at all!

open (Desc, "< extension.def");


You should always, yes *always*, check the return value from open():

open (Desc, "< extension.def") or die "could not open 'extension.def' $!";

my %tabext;
while (<Desc>){


chomp; # missing this is likely what is causing the lookup to fail

$tabext{$_}='1';
}

find(\&accounts, $direct);

sub accounts {
$File::Find::name =~ /(\..*)$/;
my $ficext = $1;


You should never use the dollar-digit variables unless you have
first ensured that the match *succeeded*.

die "no extension on '$_'" unless /(\..*)$/;
my $ficext = $1; # $1 is now safe to use

#Pour eliminer les dossiers
if (-d $File::Find::name) {}
#Pour eliminer les fichiers ayant une extension étant dans la liste
elsif ($tabext{$ficext} != undef) {}


Your hash keys have newlines, $ficext does not have a newline...
 
A

Anno Siegel

Orion93 said:
Hi!

I use File::Find and actually my script edit all files in a treefiles but
don't exclude the files with extension define in extension.def.

The line "elsif ($tabext{$ficext} != undef) {}" doesn't work, I am not able

That's not the way to test for undefined values. Use defined():

elsif ( defined $tabext{$ficext} ) { ...

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top