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

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

am 02.10.2007 15:49:19 von mosfet

Hi,

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

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

Here is my script :


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

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


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

????
system(@args) == 0 or die "system @args failed: $?";



print "\\Program Files\\Voxmobili\\Voxsync\\", $listitem, ";",
$extension, ";", $filesize;
print ";", ";", ";", ";", ";", "\n";
}

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

am 02.10.2007 15:56:45 von mosfet

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

sub IsValidExt(\@)
{
my @validExt = ("exe", "mui", "dll" );
my(@arr) = @{(shift)};
my($temp);
foreach $temp (@arr)
{
// now how can do a non sensitive case comparison
// and return a boolean
}
}

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

am 02.10.2007 17:39:16 von paduille.4061.mumia.w+nospam

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

This should get you started.

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

Read "perldoc perlre"

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

am 04.10.2007 11:06:05 von Kneo Fang

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 @validExt = (".exe", ".mui", ".dll" );
>> sub IsValidExt
>> {
>> ??????
>> }
>>
>> Here is my script :
>>
>>
>> opendir MYDIR, ".";
>> my @contents = grep !/^\.\.?$/, readdir MYDIR;
>> my @validExt = (".exe", ".mui", ".dll" );
>> [...]
>
> This should get you started.
>
> foreach my $listitem (@contents) {
> if (not $listitem =~ /\.(exe|mui|dll)$/) { next }
> print $listitem, "\n";
> }
>
> Read "perldoc perlre"

my @validExt = (".exe", ".mui", ".dll");

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

May be you want another method:

my @validExt = (".exe", ".mui", ".dll");

sub HasValidExt
{
my $ext = shift;
my $re = join ("|", @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

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

am 04.10.2007 11:33:29 von rvtol+news

Kneo Fang schreef:

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

my $re = join "|", map(quotemeta($_), @validExt);

--
Affijn, Ruud

"Gewoon is een tijger."

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

am 07.10.2007 23:28:39 von Janwillem Borleffs

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

sub IsValidext {
my $ext = shift;
return grep { /^$ext$/ } @validExt;
}


JW

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

am 08.10.2007 01:50:37 von Dummy

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

$ perl -le'
my @validExt = ( ".exe", ".mui", ".dll" );
sub IsValidext {
my $ext = shift;
return grep { /^$ext$/ } @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, @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