Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Process File Different Way Based on Passed in Argument

by ChrisCK (Initiate)
on Jan 22, 2016 at 00:12 UTC ( [id://1153332]=perlquestion: print w/replies, xml ) Need Help??

ChrisCK has asked for the wisdom of the Perl Monks concerning the following question:

I want to pass in an argument so that the script only looks for a certain pattern. This is not working for me. Something about my 'If' statements is erroring.

If I remove all the options and 'IF' statement, the actual printing of the error message works but I want to choose what it will be searching for.

use strict; use warnings; my $file = $ARGV[0]; my $purpose = $ARGV[1]; open( FILE, "$file" ) or die "Can't open $file: $!"; while ( <FILE> ) { If ($purpose = "sqlerrors") { print if ( /SQL Error:/i .. m{^\s*$}); } If ($purpose = "othererrors") { print if ( /OTHER Error:/i .. m{^\s*$}); } If ($purpose = "missingfile") { print if ( /Missing File:/i .. m{^\s*$}); } } close FILE;

Replies are listed 'Best First'.
Re: Process File Different Way Based on Passed in Argument
by davido (Cardinal) on Jan 22, 2016 at 00:17 UTC

    In each case of:

    If ($foo = "bar") {...}

    ...there are two errors.

    1. It should be spelled "if", not "If".
    2. It uses "=", which assigns a value to $purpose when it should be using "==" "eq", which compares a string value to $purpose.

    Update: Fixed the == vs eq problem. Thanks for those who pointed it out.


    Dave

        Absolutely. Good catch. :)


        Dave

Re: Process File Different Way Based on Passed in Argument
by vinoth.ree (Monsignor) on Jan 22, 2016 at 04:00 UTC
    Hi,

    If you use '==' to compare strings you will get error as below

    Argument "sqlerrors" isn't numeric in numeric eq (==) at args.pl line 11, <FILE> line 1.
    Argument "sqlerrors" isn't numeric in numeric eq (==) at args.pl line 11, <FILE> line 1.
    Argument "othererrors" isn't numeric in numeric eq (==) at args.pl line 17, <FILE> line 1.
    Argument "missingfile" isn't numeric in numeric eq (==) at args.pl line 23, <FILE> line 1.
    

    eq is for comparing strings; == is for comparing numbers.

    == does a numeric comparison: it converts both arguments to a number and then compares them.

    eq does a string comparison: the two arguments must match lexically (case-sensitive)


    All is well. I learn by answering your questions...

      How to remember that

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: Process File Different Way Based on Passed in Argument
by Anonymous Monk on Jan 22, 2016 at 15:36 UTC
    Thank you all for your help, so simple but I wouldn't have thought of it! Thanks again!!!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1153332]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-04-25 20:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found