Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Search using a variable in a Perl Script

Reply
Thread Tools

Search using a variable in a Perl Script

 
 
Philip Scobie
Guest
Posts: n/a
 
      07-03-2004
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!
 
Reply With Quote
 
 
 
 
norfernuman
Guest
Posts: n/a
 
      07-03-2004
Philip Scobie wrote:
> 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'
 
Reply With Quote
 
 
 
 
Bob Walton
Guest
Posts: n/a
 
      07-04-2004
Philip Scobie wrote:

> 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.


> 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!
>


--
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl

 
Reply With Quote
 
Philip Scobie
Guest
Posts: n/a
 
      07-04-2004
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.
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      07-04-2004
Philip Scobie wrote:
> 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?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
gnari
Guest
Posts: n/a
 
      07-04-2004
"Philip Scobie" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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




 
Reply With Quote
 
Philip Scobie
Guest
Posts: n/a
 
      07-08-2004
Many thanks.

The variable had a space after it.

Somewhat of a red face on this end... Thanks
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem in passing values to perl script using another perl script vikrant Perl Misc 4 08-10-2005 06:20 PM
search within a search within a search - looking for better way...my script times out Abby Lee ASP General 5 08-02-2004 04:01 PM
problem calling perl script from SOAP server perl script pj Perl Misc 3 04-09-2004 10:23 PM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM
How to make Perl Script "POST" call from another Perl Script??? Wet Basement Perl 1 07-15-2003 10:25 PM



Advertisments