Search using a variable in a Perl Script

P

Philip Scobie

I am searching "myfile.txt" which looks like this:

Perl.1234
HTML.1234
PHP.1234
CGI.1234

I am trying to print a single line based on a variable name, such as
"Perl" or "HTML". Maybe if I show you my code it will make more sense:


my $value = param ('incoming_parameter') (e.g. 'Perl')

open (LOG, "myfile.txt");
while (<LOG>){
if(/$value/){ <-Search for the word "Perl"???
print $_; <-If matched, print the current line.
}
}


With an 'incoming_parameter' of "Perl", it should match and print
"Perl.1234". But it doesn't.

HELP!
 
N

norfernuman

Philip said:
I am searching "myfile.txt" which looks like this:

Perl.1234
HTML.1234
PHP.1234
CGI.1234

I am trying to print a single line based on a variable name, such as
"Perl" or "HTML". Maybe if I show you my code it will make more sense:


my $value = param ('incoming_parameter') (e.g. 'Perl')

open (LOG, "myfile.txt");
while (<LOG>){
if(/$value/){ <-Search for the word "Perl"???
print $_; <-If matched, print the current line.
}
}


With an 'incoming_parameter' of "Perl", it should match and print
"Perl.1234". But it doesn't.

HELP!

Google this group archive for 'Tad', 'test open'
 
B

Bob Walton

Philip said:
I am searching "myfile.txt" which looks like this:

Perl.1234
HTML.1234
PHP.1234
CGI.1234

I am trying to print a single line based on a variable name, such as
"Perl" or "HTML". Maybe if I show you my code it will make more sense:


my $value = param ('incoming_parameter') (e.g. 'Perl')


Your "code" doesn't compile. Don't say you're showing us your code if
you aren't.

open (LOG, "myfile.txt");


How do you know your open statement is succeeding? I'll bet when your
CGI script runs, it's not running with the current directory set where
you think it is. Modify the above to something like:

open LOG,'myfile.txt' or die "Oops, couldn't open myfile.txt, $!";

and

use CGI::Carp qw(fatalsToBrowser);

Then when you see the file isn't there, specify the absolute path to
your data file when you open it.
 
P

Philip Scobie

Okay, my code is a sloppy (I was showing the relevant bits)...my bad!

But, it works if I put a non-variable in the search parameter:


if(/\./){ <- picks up ALL the entries because it matches the "."

so why, when I put in a variable, does it match nothing.


if (/$value/){ <-picks up nothing

Again, apologies for the sloppy code.
 
G

Gunnar Hjalmarsson

Philip said:
it works if I put a non-variable in the search parameter:

if(/\./){ <- picks up ALL the entries because it matches the "."

so why, when I put in a variable, does it match nothing.

if (/$value/){ <-picks up nothing

Maybe because $value does not contain what you think it does? Have you
tried

print $value;

in order to check that?
 
G

gnari

Philip Scobie said:
Okay, my code is a sloppy (I was showing the relevant bits)...my bad!

remember, if you post sloppy code where there has been no
effort made to prove that the problem is what you claim it
to be , everyone will just assume that you are an idiot
and that you untested open is to blame.
But, it works if I put a non-variable in the search parameter:


if(/\./){ <- picks up ALL the entries because it matches the "."

so why, when I put in a variable, does it match nothing.

we have no proof this variable contains what you claim.
to prove that this is indeed the problem, add this:
$value='Perl';
just before the 'if (/$value/) {' line

you you still have problems, add a bunch of debugging
prints inside your loop, showing all relevant values
($value,$_ and the result of your test)

my guess is that $value does not contain 'Perl'

gnari
 
P

Philip Scobie

Many thanks.

The variable had a space after it.

Somewhat of a red face on this end... Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top