Home > Archive > PERL Beginners > June 2005 > if else w/ &wanted
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
if else w/ &wanted
|
|
| Brian Volk 2005-06-06, 8:55 pm |
| Hello All,
I'm working w/ the File::Find module for the first time and I'm having
problems w/ my else statement.. If the query does not match the first file
in the directory, it will print the else statement. I would like it the if
statement to continue on to the next file in the directory to see if that
matches and if so, print the if statement
I'm beginning to think I might have to use a foreach statement..... I'm
just not sure if I can do that w/ the &wanted ..?
#!/usr/bin/perl -w
use strict;
use File::Find;
use CGI qw(:standard);
my $query = param("query");
my $search_results = " <http://www,company.com/msds/>
http://www,company.com/msds/";
my $no_search_results = " <http://www.compay.com/no_msds.htm>
http:/www.compay.com/no_msds.htm";
undef $/;
find(\&wanted, '/var/www/html/msds');
sub wanted {
return if($_ =~ /^\./);
stat $File::Find::name;
if ( m/$query/i ) {
print "Location: $search_results$_\n\n";
} else {
print "Location: $no_search_results\n\n";
}
}
Thanks!
Brian Volk
HP Products
317.298.9950 x1245
<mailto:bvolk@hpproducts.com> bvolk@hpproducts.com
| |
| Offer Kaye 2005-06-07, 8:56 am |
| On 6/6/05, Brian Volk wrote:
> Hello All,
>=20
> I'm working w/ the File::Find module for the first time and I'm having
> problems w/ my else statement.. If the query does not match the first fi=
le
> in the directory, it will print the else statement. I would like it the =
if
> statement to continue on to the next file in the directory to see if that
> matches and if so, print the if statement
>=20
Hi Brian,
I'm not sure what your problem is, I think you need to re-read
"perldoc File::Find". What you are describing ("would like... to
continue on to the next file") is exactly what File::Find does. Think
of it this way: for each search path you give "find()" as an argument,
&wanted will be executed once per every file in and under the given
path.
Here's some simple code as an example:
########## begin code
use strict;
use warnings;
use File::Find;
find(\&wanted, '.');
sub wanted {
return if(m/^\./);
print "$_\n";
}
########## end code
This code will print the filename of every file (remember that
directories are considered files in Unix!) under the current
directory, except for "hidden" files.
>=20
> undef $/;
Why?
>=20
> return if($_ =3D~ /^\./);
....
> if ( m/$query/i ) {
Why use 2 forms for matching $_? Try to be consistent, it really helps
readability...
> stat $File::Find::name;
>=20
Again, why? Especially as you are calling stat in empty context,
throwing away the return value. So why call it in the first place?
HTH,
--=20
Offer Kaye
| |
| Brian Volk 2005-06-08, 3:57 am |
| >
> On 6/6/05, Brian Volk wrote:
> I'm having
> match the first file
> would like it the if
> to see if that
>
> Hi Brian,
> I'm not sure what your problem is, I think you need to re-read
> "perldoc File::Find". What you are describing ("would like... to
> continue on to the next file") is exactly what File::Find does. Think
> of it this way: for each search path you give "find()" as an argument,
> &wanted will be executed once per every file in and under the given
> path.
>
> Here's some simple code as an example:
> ########## begin code
> use strict;
> use warnings;
> use File::Find;
> find(\&wanted, '.');
> sub wanted {
> return if(m/^\./);
> print "$_\n";
> }
> ########## end code
> This code will print the filename of every file (remember that
> directories are considered files in Unix!) under the current
> directory, except for "hidden" files.
>
>
> Why?
>
> ...
>
> Why use 2 forms for matching $_? Try to be consistent, it really helps
> readability...
>
>
> Again, why? Especially as you are calling stat in empty context,
> throwing away the return value. So why call it in the first place?
>
> HTH,
> --
> Offer Kaye
Offer Kaye,
Thank you for the response.. It really cleared things up.. It turns out
all I need to do was move the last print statement outside of the sub.
Thanks again for your help.
Brian Volk
#!/usr/bin/perl -w
use strict;
use File::Find;
use CGI qw(:standard);
my $query = param("query");
my $search_results = "http://www.company.com//";
my $no_search_results = "http://www.company.com/no_msds.htm";
find(\&wanted, '/var/www/html/msds');
sub wanted {
if ( m/$query/i ) {
print "Location: $search_results$_\n\n";
}
}
print "Location: $no_search_results\n\n";
|
|
|
|
|