Search using a variable in a Perl Script

Discussion in 'Perl Misc' started by Philip Scobie, Jul 4, 2004.

  1. 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!
    Philip Scobie, Jul 4, 2004
    #1
    1. Advertising

  2. Philip Scobie

    norfernuman Guest

    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'
    norfernuman, Jul 4, 2004
    #2
    1. Advertising

  3. Philip Scobie

    Bob Walton Guest

    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
    Bob Walton, Jul 4, 2004
    #3
  4. 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.
    Philip Scobie, Jul 4, 2004
    #4
  5. 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
    Gunnar Hjalmarsson, Jul 4, 2004
    #5
  6. Philip Scobie

    gnari Guest

    "Philip Scobie" <> wrote in message
    news:...
    > 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
    gnari, Jul 4, 2004
    #6
  7. Many thanks.

    The variable had a space after it.

    Somewhat of a red face on this end... Thanks
    Philip Scobie, Jul 9, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wet Basement
    Replies:
    1
    Views:
    2,518
    BobMonk
    Jul 15, 2003
  2. dpackwood
    Replies:
    3
    Views:
    1,793
  3. Abby Lee
    Replies:
    5
    Views:
    400
    Abby Lee
    Aug 2, 2004
  4. pj
    Replies:
    3
    Views:
    284
  5. vikrant
    Replies:
    4
    Views:
    278
Loading...

Share This Page