#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0

    Question How to get lines having a variable in perl?


    Hi, This might be simple question. But i couldn't find the answer.
    #$ra is having a value. I want the first occurrence of the line in a file having the value that is in variable "$ra"


    for ex:
    $ra=000000000000096C
    Now i want the first line of a file that is having the value 000000000000096C
    like,
    I 000000000000096C 48000000 * 000000000000096C b *
    Here is the code i wrote

    33: # printing the iop corresponding to real address
    34: open (TST, '<psi.tst');
    35: while ( $iop = <TST>)
    36: {
    37: if($iop =~ /$ra/){
    38: last;
    39: }
    40: }
    41: print $iop;



    ERROR iam getting is :
    Use of uninitialized value in print at psi.pl line 41, <TST> line 51574.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    776
    Rep Power
    495
    This is poorly written. Try something like this:

    Perl Code:
    open my $TST, "<", "psi.tst" or die "could not open psi.tst $!";
    while ( my $iop = <$TST>) {
         if ($iop =~ /$ra/) {
              print $iop;
              last;
         }
    }
    close $TST;

    Comments on this post

    • keath agrees
    Last edited by Laurent_R; August 15th, 2013 at 09:27 AM.
  4. #3
  5. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,221
    Rep Power
    1809
    The reason you received the warning about an uninitialized variable is because $iop can reach the end of the script without matching or being defined. You can eliminate the warning by testing before printing:

    Code:
    print $iop if $iop;
    Would probably be better to print a notice to the user that there was no match.

    The bigger issue is you didn't use 'strict' mode, so your variable scoping isn't right. If you declare the variable within the loop:
    Code:
    while (my $iop = <DATA>) {
         if ($iop =~ /$ra/) {
              print "$.: $iop";
              last;
         }
    }
    print $iop;
    The the final print statement will be a true error, and the script won't work because $iop doesn't exist outside the loop.

    If you define $iop before hand:
    Code:
    my $iop;
    while ($iop = <DATA>) {
         if ($iop =~ /$ra/) {
              print "$.: $iop";
              last;
         }
    }
    print $iop;
    then it should be clear that since $iop was undefined before the loop, it could still be undefined afterwards and you need to test it.
    Last edited by keath; August 15th, 2013 at 09:53 AM. Reason: beep
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    776
    Rep Power
    495
    Originally Posted by keath
    Also Laurent, there is a typo in your script. $iop was declared twice by accident.
    Right, I changed it.

IMN logo majestic logo threadwatch logo seochat tools logo