Solved

Perl IF statement

Posted on 1998-07-23
22
208 Views
Last Modified: 2010-03-05
I have written an if statement to go and search a file using the information contained in a variable.
The If statement is as follows:

        if(/$search/i)
        {
          $inloop = $search;
          my @test=split(/\|/, $_);
          $array[$a] = new $req(@test);
          $a++;
         }

Before the IF statement the variable search contains the number = 901114920
However when inside the if statement, $search loses it's value (as tested by the variable $inloop).
If I put the number 901114920 where $search is located like so:

        if(/901114920/i)
        {
          $inloop = $search;
          my @test=split(/\|/, $_);
          $array[$a] = new $req(@test);
          $a++;
         }

The statement works.

Any clues why it's not working with the variable, but is with the number. I have used a variable in this if statement and it has worked other times.

HELP ME!!!!

Thanks, Rene
0
Comment
Question by:rrivers
  • 8
  • 5
  • 5
  • +2
22 Comments
 
LVL 5

Expert Comment

by:b2pi
ID: 1208680
Have you tried using perl -w and use strict?

What's setting $_?


0
 

Author Comment

by:rrivers
ID: 1208681
The $_ represents the current array item. and I have tried the debugger and it comes up with no problems with the script.
0
 

Author Comment

by:rrivers
ID: 1208682
$_ is being set by the reading of the file that SEARCH (not $search) represents
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 6

Expert Comment

by:alamo
ID: 1208683
Let me restate what I think you are saying - with

if(/$search/i)
{
   $inloop = $search;

The if properly executes if $_ contains 901114920, but after the if block $inline isn't 901114920. (What is it?)

As opposed to:

if(/901114920/i)
{
   $inloop = $search;
where the if properly executes if $_ contains 901114920, and  after the if block $inline is 901114920.

Is that what you are saying?
0
 
LVL 6

Expert Comment

by:alamo
ID: 1208684
$inline was a typo for $inloop, sorrry.
0
 

Author Comment

by:rrivers
ID: 1208685
Let me clarify what is going on here by giving you some more code.

sub search{
 
$req=$_[1];
require ($req.".pm");
 $a=0;
 
open (SEARCH,$_[0]) || die;
 
 &lock(SEARCH);        # Lock the file so nobody else uses it.
 while(<SEARCH>)       # Loop through the file.
   {
      $_=~s/\"//g;     # Gets rid of quotes in the database.
     $outloop = $search;
     if(/$search/i){
        $inloop = $search;
        my @test=split(/\|/, $_);
        $array[$a] = new $req(@test);
        $a++;
      }
 
   } # End of While loop, so we must be at the end of the file.
 &unlock(SEARCH);
 close(SEARCH);
 return;
}

This compares $search with $_ (which is part of a line read in from SEARCH). And before you ask, the 2 searches are not getting confused because I can change the name of the variable $search to something else and it still does not work.

$outloop contains the number: 901114920 which is read in from another array. So before the if statement $search does contain the correct data.

$inloop is empty. Which means that for some reason the if statement does not like $search. however if I replace $search as so:
if(/901114920/i)
it works fine.

I would say the problem was the fact that the if statement does not like variables, however I have used this same routine with other variables and it works fine.

Trust me I used all 200 points for a reason!!!
0
 
LVL 5

Expert Comment

by:b2pi
ID: 1208686
What version of perl are you using?
0
 
LVL 5

Expert Comment

by:b2pi
ID: 1208687
By the way

/$variable/

_will_ interpolate.
0
 

Author Comment

by:rrivers
ID: 1208688
Perl 5.004_04

By the way /$variable/  _will_ interpolate -----???????
0
 
LVL 5

Expert Comment

by:b2pi
ID: 1208689
I was sort of hoping that you were going to say 5.001...

yes, /$variable/ will interpolate, at least, so says perlop

Ummm, that means that
$a = 'q';
$z = /$a/;
$x = /q/;

$z and $x will be equal.
0
 

Author Comment

by:rrivers
ID: 1208690
Yes, but that still doesn't answer my question! :)
0
 
LVL 5

Expert Comment

by:b2pi
ID: 1208691
Personally, I think something is going on here that we don't see.  Your code works here, and, I suspect, alamo feels the same way.

1.) Is the match actually succeeding? (i.e. is $inloop really being assigned when you use /$search/i  )
2.) What happens if you assign $inloop to $_ ?
0
 
LVL 84

Expert Comment

by:ozo
ID: 1208692
Are you sure $search really contains "901114920"?
0
 

Author Comment

by:rrivers
ID: 1208693
The match is succeding if I don't use the variable, but instead I use a number.
If I assign $inloop to $_ I am going to get the lines of the file it is reading.

Let me tell you exactly what is going on. I have a form. The user clicks on a radio button that has a value attached to it(that value is the number that $search becomes). A perl script then reads the input and puts it into that array (@input). @input[0] is always the number I want to do a search on. So I assign that to the variable $search. It passes it on to the search subroutine. I check at the beginning on the subroutine to make sure that $search is indeed equal to the number I want (That is $outloop). I then check to make sure that $search is equal to that same number inside the loop after the if condition is read (This is $inloop). This is where something goes wrong. $inloop loses it's value.

How ever if I hard code the number $search is equal to "901114920". the search successfully completes and gives me my correct output.

That is everything. There is nothing else going on here, no tricks. This is why I am so stumped, it makes no sense!!!!!!
0
 
LVL 84

Expert Comment

by:ozo
ID: 1208694
Is there a difference between

$search = '901114920';
if( /$search/i ){
             $inloop = $search;
             my @test=split(/\|/, $_);
             $array[$a] = new $req(@test);
             $a++;
}

and

if( /901114920/i ){
             $inloop = 901114920;
             my @test=split(/\|/, $_);
             $array[$a] = new $req(@test);
             $a++;
}
0
 

Author Comment

by:rrivers
ID: 1208695
What is the difference?
Either one should correctly complete the search.
0
 
LVL 6

Expert Comment

by:alamo
ID: 1208696
I can't tell precisely how you set $search, but I think there's a possibilitythat $seach looks like 901114920 to you but isn't really, i.e. there's some non-printing character there (like a terminating \n") that isn't clear to you. Therefore the search fails, and that's why $inloop is blank (it's never set).

Try checking ($search eq '901114920') at the start of the function. If it's false, that might be it.

chomp $search might also be a good idea.
0
 

Author Comment

by:rrivers
ID: 1208697
Thank you so much!!!! It should of figured that it was adding extra characters at the end of the number (2 blanks in fact!). So I just used substr to get rid of them.

Doing a happy dance now!!!!

Rene

Now how do you get the point credit for this?
0
 
LVL 6

Expert Comment

by:alamo
ID: 1208698
Great!

In order to receive points, one of us needs to post an answer which you can then grade. But this really should be split 3 ways, and Experts Exchange hasn't yet given you an automatic way to split points. What you should do is go to the Customer Service topic area and post a 0 point question for Linda telling her you want to split points 3 ways for this question (give this question number). She'll probably give you some extra points so that you can post dummy questions for b2pi and ozo to get them some points, and then I'll lock this one.

Glad we could help!
0
 
LVL 7

Expert Comment

by:linda101698
ID: 1208699
alamo,
Post an answer to the question.  I'll add points to rrivers account so he can post questions for ozo and b2pi.  Thanks to all for your help on this question.  TEAMWORK!  GREAT JOB :-)

Linda Gardner
Customer Service @ Experts Exchange
0
 
LVL 6

Accepted Solution

by:
alamo earned 200 total points
ID: 1208700
Thanks Rene, it's great when someone you help appreciates it!
0
 
LVL 84

Expert Comment

by:ozo
ID: 1208701
Having settled the problem of what $search contains, there are a few other comments that seem worth making:
$search =~ s/\s+$//; #is an easy way to remove blanks from the end.
If $search can contain characters like '()[].*?+', it may be safer to use /\Q$search$E/
But rather than recompile the regular expression for each line of <SEARCH>
it may be better to use
 $search = lc $search;
 (index(lc($_),$search) >= 0
An easier way to append to an array would be
@array = ();
while( <SEARCH> ){
  push @array, new $req(split(/\|/);
}
If you unlock the file before closing it, someone else may lock the file before the close can flush any buffers you've writen.
It's usually better practice to let unlock be done by the close
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question