unable to remove the ./ when using File::Find::name

Hi

I have script where i need to go in to a directory and put all files
in to a array

if ( chdir ("$dirtemp") ) {
find (sub { push [at] all , $File::Find::name}, ".");

my %selectfiles = qw( /classes/mail.class.php
classes/dealer.class.php
classes/memcache.class.php
classes/phpmailer
classes/phpmailer/.htaccess
classes/phpmailer/class.phpmailer.php
classes/phpmailer/class.smtp.php
classes/phpmailer/ChangeLog.txt
classes/phpmailer/language
);

foreach ( [at] all){
if ( defined $selectfiles{$_} ){
push ( [at] filesfinal , $_);
}
}

this obviously will not work cause of the ./ in the File::Find
function , how can i work around this .

Thanks


--
Regards
Agnello D'souza

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Agnello George [ Mi, 20 April 2011 11:50 ] [ ID #2058441 ]

Re: unable to remove the ./ when using File::Find::name

On Wednesday 20 Apr 2011 12:50:45 Agnello George wrote:
> Hi
>
> I have script where i need to go in to a directory and put all files
> in to a array
>
> if ( chdir ("$dirtemp") ) {
> find (sub { push [at] all , $File::Find::name}, ".");
>
> my %selectfiles = qw( /classes/mail.class.php
> classes/dealer.class.php
> classes/memcache.class.php
> classes/phpmailer
> classes/phpmailer/.htaccess
> classes/phpmailer/class.phpmailer.php
> classes/phpmailer/class.smtp.php
> classes/phpmailer/ChangeLog.txt
> classes/phpmailer/language
> );
>
> foreach ( [at] all){
> if ( defined $selectfiles{$_} ){
> push ( [at] filesfinal , $_);
> }
> }
>
> this obviously will not work cause of the ./ in the File::Find
> function , how can i work around this .
>

You can remove the ./:

[CODE]
foreach my $filename ( [at] all)
{
my $fn_wo_prefix = $filename;
$fn_wo_prefix =~ s{\A\./}{};

if (exists($selectfiles{$fn_wo_prefix}))
{
push [at] final_files, $filename;
}
}
[/CODE]

A few more comments:

1. You should use exists instead of defined.

2. You initialise the hash incorrectly. You need:

my %selected_files = (map { $_ => 1 } [at] FILES);

3. See http://perldoc.perl.org/functions/grep.html

4. "$dirtemp" should be $temp_dir without the quotes.

5. Use some underscores in your identifiers.

Regards,

Shlomi Fish

--
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
What Makes Software Apps High Quality - http://shlom.in/sw-quality

I hope that you agree with me that 99.9218485921% of the users wouldn't bother
themselves with recompilation (or any other manual step for that matter) to
make their games run 1.27127529900685765% faster ;-) -- Nadav Har'El

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Shlomi Fish [ Mi, 20 April 2011 12:44 ] [ ID #2058442 ]

Re: unable to remove the ./ when using File::Find::name

On Wed, Apr 20, 2011 at 4:14 PM, Shlomi Fish <shlomif [at] iglu.org.il> wrote:
> On Wednesday 20 Apr 2011 12:50:45 Agnello George wrote:
>> Hi
>>
>> I have script where i need to =A0go in to a directory and put all files
>> in to a array
>>
>> if ( chdir ("$dirtemp") ) {
>> =A0find (sub { push [at] all , $File::Find::name}, ".");
>>
>> =A0my %selectfiles =3D qw( /classes/mail.class.php
>> classes/dealer.class.php
>> classes/memcache.class.php
>> classes/phpmailer
>> classes/phpmailer/.htaccess
>> classes/phpmailer/class.phpmailer.php
>> classes/phpmailer/class.smtp.php
>> classes/phpmailer/ChangeLog.txt
>> classes/phpmailer/language
>> =A0);
>>
>> foreach ( [at] all){
>> =A0 =A0 if ( defined $selectfiles{$_} ){
>> =A0 =A0 =A0 push ( [at] filesfinal , $_);
>> =A0 =A0 =A0 }
>> =A0 =A0 }
>>
>> this obviously will not work cause of the ./ in the File::Find
>> function , how can i work around this .
>>
>
> You can remove the ./:
>
> [CODE]
> foreach my $filename ( [at] all)
> {
> =A0 =A0 =A0 =A0my $fn_wo_prefix =3D $filename;
> =A0 =A0 =A0 =A0$fn_wo_prefix =3D~ s{\A\./}{};
>
> =A0 =A0 =A0 =A0if (exists($selectfiles{$fn_wo_prefix}))
> =A0 =A0 =A0 =A0{
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0push [at] final_files, $filename;
> =A0 =A0 =A0 =A0}
> }
> [/CODE]
>
> A few more comments:
>
> 1. You should use exists instead of defined.
>
> 2. You initialise the hash incorrectly. You need:
>
> my %selected_files =3D (map { $_ =3D> 1 } [at] FILES);
>
> 3. See http://perldoc.perl.org/functions/grep.html
>
> 4. "$dirtemp" should be $temp_dir without the quotes.
>
> 5. Use some underscores in your identifiers.
>
> Regards,
>
> =A0 =A0 =A0 =A0Shlomi Fish
>
> --

Thanks a lot , but was just wondering if i could ignore it in the
File::Find function



--
Regards
Agnello D'souza

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Agnello George [ Mi, 20 April 2011 12:55 ] [ ID #2058443 ]

Re: unable to remove the ./ when using File::Find::name

On Wed, Apr 20, 2011 at 4:14 PM, Shlomi Fish <shlomif [at] iglu.org.il> wrote:
> On Wednesday 20 Apr 2011 12:50:45 Agnello George wrote:
>> Hi
>>
>> I have script where i need to =A0go in to a directory and put all files
>> in to a array
>>
>> if ( chdir ("$dirtemp") ) {
>> =A0find (sub { push [at] all , $File::Find::name}, ".");
>>
>> =A0my %selectfiles =3D qw( /classes/mail.class.php
>> classes/dealer.class.php
>> classes/memcache.class.php
>> classes/phpmailer
>> classes/phpmailer/.htaccess
>> classes/phpmailer/class.phpmailer.php
>> classes/phpmailer/class.smtp.php
>> classes/phpmailer/ChangeLog.txt
>> classes/phpmailer/language
>> =A0);
>>
>> foreach ( [at] all){
>> =A0 =A0 if ( defined $selectfiles{$_} ){
>> =A0 =A0 =A0 push ( [at] filesfinal , $_);
>> =A0 =A0 =A0 }
>> =A0 =A0 }
>>
>> this obviously will not work cause of the ./ in the File::Find
>> function , how can i work around this .
>>
>
> You can remove the ./:
>
> [CODE]
> foreach my $filename ( [at] all)
> {
> =A0 =A0 =A0 =A0my $fn_wo_prefix =3D $filename;
> =A0 =A0 =A0 =A0$fn_wo_prefix =3D~ s{\A\./}{};
>
> =A0 =A0 =A0 =A0if (exists($selectfiles{$fn_wo_prefix}))
> =A0 =A0 =A0 =A0{
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0push [at] final_files, $filename;
> =A0 =A0 =A0 =A0}
> }
> [/CODE]
>
> A few more comments:
>
> 1. You should use exists instead of defined.
>
> 2. You initialise the hash incorrectly. You need:
>
> my %selected_files =3D (map { $_ =3D> 1 } [at] FILES);
>
> 3. See http://perldoc.perl.org/functions/grep.html
>
> 4. "$dirtemp" should be $temp_dir without the quotes.
>
> 5. Use some underscores in your identifiers.
>

in what you mentioned what does \A stand for ??

$fn_wo_prefix =3D~ s{\A\./}{};


--
Regards
Agnello D'souza

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Agnello George [ Mi, 20 April 2011 12:59 ] [ ID #2058444 ]

Re: unable to remove the ./ when using File::Find::name

Hi Agnello,

On Wednesday 20 Apr 2011 13:59:24 Agnello George wrote:
> On Wed, Apr 20, 2011 at 4:14 PM, Shlomi Fish <shlomif [at] iglu.org.il> wrote:
> > On Wednesday 20 Apr 2011 12:50:45 Agnello George wrote:
> >
> > You can remove the ./:
> >
> > [CODE]
> > foreach my $filename ( [at] all)
> > {
> > my $fn_wo_prefix = $filename;
> > $fn_wo_prefix =~ s{\A\./}{};
> >
> > if (exists($selectfiles{$fn_wo_prefix}))
> > {
> > push [at] final_files, $filename;
> > }
> > }
> > [/CODE]
>
> in what you mentioned what does \A stand for ??
>
> $fn_wo_prefix =~ s{\A\./}{};

\A stands for the beginning of the string:

http://perldoc.perl.org/perlreref.html :

"\A Match string start (regardless of /m)"

See the links on:

http://perl-begin.org/topics/regular-expressions/

Regards,

Shlomi Fish

--
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
My Public Domain Photos - http://www.flickr.com/photos/shlomif/

Chuck Norris doesn't make mistakes. (Su-Shee) He corrects God. (Shlomi Fish)

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Shlomi Fish [ Mi, 20 April 2011 13:04 ] [ ID #2058445 ]

Re: unable to remove the ./ when using File::Find::name

On Wed, Apr 20, 2011 at 4:34 PM, Shlomi Fish <shlomif [at] iglu.org.il> wrote:
> Hi Agnello,
>
> On Wednesday 20 Apr 2011 13:59:24 Agnello George wrote:
>> On Wed, Apr 20, 2011 at 4:14 PM, Shlomi Fish <shlomif [at] iglu.org.il> wrote=
:
>> > On Wednesday 20 Apr 2011 12:50:45 Agnello George wrote:
>> >
>> > You can remove the ./:
>> >
>> > [CODE]
>> > foreach my $filename ( [at] all)
>> > {
>> > =A0 =A0 =A0 =A0my $fn_wo_prefix =3D $filename;
>> > =A0 =A0 =A0 =A0$fn_wo_prefix =3D~ s{\A\./}{};
>> >
>> > =A0 =A0 =A0 =A0if (exists($selectfiles{$fn_wo_prefix}))
>> > =A0 =A0 =A0 =A0{
>> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0push [at] final_files, $filename;
>> > =A0 =A0 =A0 =A0}
>> > }
>> > [/CODE]
>>
>> in what =A0you mentioned =A0what does \A stand for ??
>>
>> =A0 $fn_wo_prefix =3D~ s{\A\./}{};
>
> \A stands for the beginning of the string:
>
> http://perldoc.perl.org/perlreref.html :
>
> "\A Match string start (regardless of /m)"
>
> See the links on:
>
> http://perl-begin.org/topics/regular-expressions/
>
> Regards,
>
> =A0 =A0 =A0 =A0Shlomi Fish
>
> --
> ------------------------------------------------------------ -----
> Shlomi Fish =A0 =A0 =A0 http://www.shlomifish.org/
> My Public Domain Photos - http://www.flickr.com/photos/shlomif/
>
> Chuck Norris doesn't make mistakes. (Su-Shee) He corrects God. (Shlomi Fi=
sh)
>
> Please reply to list if it's a mailing list post - http://shlom.in/reply =
..
>


ok thanks

--
Regards
Agnello D'souza

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Agnello George [ Mi, 20 April 2011 13:10 ] [ ID #2058446 ]

Re: unable to remove the ./ when using File::Find::name

On 11-04-20 06:55 AM, Agnello George wrote:
> Thanks a lot , but was just wondering if i could ignore it in the
> File::Find function
>

Yup.

my [at] all = qw( /classes/mail.class.php
classes/dealer.class.php
classes/memcache.class.php
classes/phpmailer
classes/phpmailer/.htaccess
classes/phpmailer/class.phpmailer.php
classes/phpmailer/class.smtp.php
classes/phpmailer/ChangeLog.txt
classes/phpmailer/language
);

for ( [at] all ){
if( -f ){
push [at] filesfinal, $_;
}
}

Alternatively, using grep:

[at] filesfinal = grep { -f } [at] all;


--
Just my 0.00000002 million dollars worth,
Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software: Fail early & often.

Eliminate software piracy: use only FLOSS.

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Shawn H Corey [ Mi, 20 April 2011 14:41 ] [ ID #2058448 ]
Perl » gmane.comp.lang.perl.beginners » unable to remove the ./ when using File::Find::name

Vorheriges Thema: Perl source code beautifier
Nächstes Thema: How do I get the content-disposition of a MIME::Entity part