Bookmarks

Yahoo Gmail Google Facebook Delicious Twitter Reddit Stumpleupon Myspace Digg

Search queries

sqldatasource dal, wwwxxxenden, convert raid5 to raid 10 mdadm, apache force chunked, nrao wwwxxx, xxxxxdup, procmail change subject header, wwwXxx not20, Wwwxxx.doks sas, linux raid resync after reboot

Links

XODOX
Impressum

#1: How can I check if a string belongs to a string array

Posted on 2007-10-02 15:49:19 by 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";
}

Report this message

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

Posted on 2007-10-02 15:56:45 by 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
}
}

Report this message

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

Posted on 2007-10-02 17:39:16 by 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"

Report this message

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

Posted on 2007-10-04 11:06:05 by 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

Report this message

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

Posted on 2007-10-04 11:33:29 by 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."

Report this message

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

Posted on 2007-10-07 23:28:39 by Janwillem Borleffs

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

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


JW

Report this message

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

Posted on 2007-10-08 01:50:37 by 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

Report this message