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

the perl script is meant to do something as described below I have a file , i then split the lines ( in the file) into arrays so that I can use a value from that line which then prints out a statement when a particular number appears on a line, but my script seems to be prinintg out all the statements for everyline, see below
#!/perl/bin open (In,"tascomod.log") or die ("can not open file at the moment"); open (Out, "tasmodtypfinrpt.txt") or die ("cannot open output file may +be you should create one"); #print Out " TXSpeed Rx Speed"; while ($line=) { @temp = split(/[=]/,$line); #if ($temp[1] >100) # { # print "Session id is $temp[1] with "; # } if ($temp[1]>=1&&($temp[1]<=45)) { $modtyp = $temp[1]; print "\n$modtyp\n"; #the above line prints out ok but does not seem to then print out the +statement which corresponds with the value instead #it prints out all the statements, even the ones that do not correspon +d to the value! if ($modtyp = 20) { print "V34\n"; } if ($modtype = 9) { print "no connection\n"; } elsif ($modtype = 34) { print "V90\n"; } } }
the results looks like the following;
9 V34 no connection 34 V34 no connection 9 V34 no connection 45 V34 no connection 45 V34 no connection 7 V34 no connection

Replies are listed 'Best First'.
Re: printing options with elsif statement
by jmcnamara (Monsignor) on Feb 18, 2002 at 17:05 UTC

    The statements
    if ($modtyp = 20) # etc
    should be
    if ($modtyp == 20) # etc

    If you use perl -w you will get a warning about this type of error:    Found = in conditional, should be == at myprog.pl line x.

    Your shebang line looks a bit odd. It should be something like:     #!/usr/bin/perl -w There are other errors in your code as well. If you enable warnings, as shown above, the compiler will highlight most of them.

    --
    John.

Re: printing options with elsif statement
by MZSanford (Curate) on Feb 18, 2002 at 17:08 UTC
    As a side note, if you are planning to print to the Out filehandle, you need yo open it in write mode (see here).
    from the frivolous to the serious
Re: printing options with elsif statement
by dash2 (Hermit) on Feb 18, 2002 at 17:16 UTC

    You're confusing equality (==) with assignment (=) - an easy mistake to make even for experienced programmers.

    if ($foo == 3) checks if $foo is equal to 3.

    if ($foo = 3) gives $foo a value of 3. This assignment then itself evaluates to 3, which is "true", so the if statement is always true.

    dave hj~

      ok I have changed it to check whether the variable $modtyp is x,
      $modtyp = 9; if ($modtyp == 9) { print " response1"; } elsif ($modtyp == 10) { print " response2\n"; } the reponses are not being typed even if the value of $modtyp can be seen to be set to a value being checked!!!
        Well, that code snippet works... I just tried it.

        dash2@cherokee:~ > perl $modtyp = 9; if ($modtyp == 9) { print " response1"; } elsif ($modtyp == 10) { print " response2\n"; } response1dash2@cherokee:~ >
        So, as Sherlock Holmes said, once you have eliminated the impossible, whatever remains, however improbable, must be true. Check elsewhere in your code. Check you are printing newlines, cos sometimes perl doesn't print out until you hit a newline. Check you are printing to where you think you are - did you select some other filehandle? Check that $modtyp is exactly what you think it is by doing print "modtyp is '$modtyp'". Debug. Fix. Good luck.

        dave hj~

Re: printing options with elsif statement
by lordsuess (Scribe) on Feb 21, 2002 at 18:01 UTC
    It might be better to write Filehandles in all UPPERCASE, because if you use perl -w, it will tell you some warnings. If any errors happen with open, they are in the special variable $!
    E.g:
    open (IN, "tascomod.log") or die ("can not open file: !"); open (OUT, ">tasmodtypfinrpt.txt") or die ("cannot open output: $!");