How can I check if a string belongs to a string array

Hi,

I need a function to check if a file has the right extension

my [at] validExt = (".exe", ".mui", ".dll" );
sub IsValidExt
{
??????
}

Here is my script :


opendir MYDIR, ".";
my [at] contents = grep !/^\.\.?$/, readdir MYDIR;
my [at] validExt = (".exe", ".mui", ".dll" );

foreach my $listitem ( [at] contents )
{
$extension = substr( $listitem, rindex( $listitem, '.' ) + 1 );
$filesize = stat($listitem)->size;
$totalsize += $filesize;
my [at] args = ($DumpBin, $listitem );


#System need to be called if file has valid extension !!!!!!!!!!!!!

????
system( [at] args) == 0 or die "system [at] args failed: $?";



print "\\Program Files\\Voxmobili\\Voxsync\\", $listitem, ";",
$extension, ";", $filesize;
print ";", ";", ";", ";", ";", "\n";
}
mosfet [ Di, 02 Oktober 2007 15:49 ] [ ID #1834357 ]

Re: How can I check if a string belongs to a string array

mosfet a écrit :
> Hi,
>
> I need a function to check if a file has the right extension
>
> my [at] validExt = (".exe", ".mui", ".dll" );
> sub IsValidExt
> {
> ??????
> }
>
> Here is my script :
>
>
> opendir MYDIR, ".";
> my [at] contents = grep !/^\.\.?$/, readdir MYDIR;
> my [at] validExt = (".exe", ".mui", ".dll" );
>
> foreach my $listitem ( [at] contents )
> {
> $extension = substr( $listitem, rindex( $listitem, '.' ) + 1 );
> $filesize = stat($listitem)->size;
> $totalsize += $filesize;
> my [at] args = ($DumpBin, $listitem );
>
>
> #System need to be called if file has valid extension !!!!!!!!!!!!!
>
> ????
> system( [at] args) == 0 or die "system [at] args failed: $?";
>
>
>
> print "\\Program Files\\Voxmobili\\Voxsync\\", $listitem, ";",
> $extension, ";", $filesize;
> print ";", ";", ";", ";", ";", "\n";
> }
I started with this :

sub IsValidExt(\ [at] )
{
my [at] validExt = ("exe", "mui", "dll" );
my( [at] arr) = [at] {(shift)};
my($temp);
foreach $temp ( [at] arr)
{
// now how can do a non sensitive case comparison
// and return a boolean
}
}
mosfet [ Di, 02 Oktober 2007 15:56 ] [ ID #1834358 ]

Re: How can I check if a string belongs to a string array

On 10/02/2007 08:49 AM, mosfet wrote:
> Hi,
>
> I need a function to check if a file has the right extension
>
> my [at] validExt = (".exe", ".mui", ".dll" );
> sub IsValidExt
> {
> ??????
> }
>
> Here is my script :
>
>
> opendir MYDIR, ".";
> my [at] contents = grep !/^\.\.?$/, readdir MYDIR;
> my [at] validExt = (".exe", ".mui", ".dll" );
> [...]

This should get you started.

foreach my $listitem ( [at] contents) {
if (not $listitem =~ /\.(exe|mui|dll)$/) { next }
print $listitem, "\n";
}

Read "perldoc perlre"
paduille.4061.mumia.w [ Di, 02 Oktober 2007 17:39 ] [ ID #1834360 ]

Re: How can I check if a string belongs to a string array

Mumia W. wrote:
> On 10/02/2007 08:49 AM, mosfet wrote:
>> Hi,
>>
>> I need a function to check if a file has the right extension
>>
>> my [at] validExt = (".exe", ".mui", ".dll" );
>> sub IsValidExt
>> {
>> ??????
>> }
>>
>> Here is my script :
>>
>>
>> opendir MYDIR, ".";
>> my [at] contents = grep !/^\.\.?$/, readdir MYDIR;
>> my [at] validExt = (".exe", ".mui", ".dll" );
>> [...]
>
> This should get you started.
>
> foreach my $listitem ( [at] contents) {
> if (not $listitem =~ /\.(exe|mui|dll)$/) { next }
> print $listitem, "\n";
> }
>
> Read "perldoc perlre"

my [at] validExt = (".exe", ".mui", ".dll");

sub IsValidExt
{
my $ext = shift;
my $re = join ("|", [at] validExt);
$re =~ s/([.])/\\\1/g;
return $ext =~ /^($re)$/;
}

May be you want another method:

my [at] validExt = (".exe", ".mui", ".dll");

sub HasValidExt
{
my $ext = shift;
my $re = join ("|", [at] validExt);
$re =~ s/([.])/\\\1/g;
return $ext =~ /($re)$/; #only matches tail
}

You can cache these regular expressions to avoid re-construction in a
second call.


Best wishes,
Kneo Fang
Kneo Fang [ Do, 04 Oktober 2007 11:06 ] [ ID #1836556 ]

Re: How can I check if a string belongs to a string array

Kneo Fang schreef:

> my $re = join ("|", [at] validExt);
> $re =~ s/([.])/\\\1/g;

my $re = join "|", map(quotemeta($_), [at] validExt);

--
Affijn, Ruud

"Gewoon is een tijger."
rvtol+news [ Do, 04 Oktober 2007 11:33 ] [ ID #1836557 ]

Re: How can I check if a string belongs to a string array

mosfet wrote:
> my [at] validExt = (".exe", ".mui", ".dll" );
> sub IsValidExt
> {
> ??????
> }
>

sub IsValidext {
my $ext = shift;
return grep { /^$ext$/ } [at] validExt;
}


JW
Janwillem Borleffs [ So, 07 Oktober 2007 23:28 ] [ ID #1838734 ]

Re: How can I check if a string belongs to a string array

Janwillem Borleffs wrote:
>
> mosfet wrote:
>>
>> my [at] validExt = (".exe", ".mui", ".dll" );
>>
>> sub IsValidExt
>> {
>> ??????
>> }
>
> sub IsValidext {
> my $ext = shift;
> return grep { /^$ext$/ } [at] validExt;
> }

$ perl -le'
my [at] validExt = ( ".exe", ".mui", ".dll" );
sub IsValidext {
my $ext = shift;
return grep { /^$ext$/ } [at] validExt;
}

print q["\.\m\u\i" is ], IsValidext( q[\.\m\u\i] ) ? q[] : q[NOT ], q[a valid
extention.];
'
"\.\m\u\i" is a valid extention.


So the eight character string '\.\m\u\i' is a valid extention?

This should work better:

sub IsValidext {
my $ext = shift;
return grep $_ eq $ext, [at] validExt;
}




John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
Dummy [ Mo, 08 Oktober 2007 01:50 ] [ ID #1839573 ]
Perl » alt.perl » How can I check if a string belongs to a string array

Vorheriges Thema: cgi script to connect to database and diaplay data in HTML throwing error
Nächstes Thema: Returns of Encode::decode / guess_encoding ?