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

    Join Date
    Jun 2012
    Posts
    82
    Rep Power
    3

    Is possible to “print ones”?


    Hi guys,
    I was wondering if it is possible to “print ones”.
    When using while loop for each not matched line I want to print just ones “line not Found” .

    Code:
    Code:
                      while (my $line = <$inf_fh>)  { 
                       
                               chomp $line ;
                               
                       if ($line =~/$parama_one/g)   {
                       print $line, "\n" ;
                       print $out_fh $line,"\n" ;                 
                       
                       }
                       else  
                       {
                       print $out_fh "$parama_one NOT FOUND\n" ; #print ones#
                       }
    
                       if ($line =~/$parama_two/g)   {
                       print $line, "\n" ;
                       print $out_fh $line,"\n" ;                 
                       
                       }
                       else  
                       {                   
                       print $out_fh "$parama_two NOT FOUND\n" ; #print ones#
                       }
              }
       exit ;
    Thank you.
    testerV
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    I assume you meant "print once" and English is not your first language. It is very simple, all you have to do is:
    Code:
                       else  
                       {
                       print $out_fh "$parama_one NOT FOUND\n" ; #print ones#
                       last;
                       }
    and same thing in the other section as well:
    Code:
                       else  
                       {                   
                       print $out_fh "$parama_two NOT FOUND\n" ; #print ones#
                       last;
                       }
    The last keyword tells it to break out of the while loop.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    831
    Rep Power
    496
    I also did not understand exactly TesterV's requirement, but I doubt that the solution proposed with the last statement really makes sense in the context, since it means the code will break out of the while loop and stop reading the file.

    Quite possibly the use of the 'next' statemlent instead of 'last' would be better, since it would lead to break out of the processing of the current line and go to the next line in the whgile loop, but it is just possible, since, as I said, I did not understand the OP's exact requirement.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,932
    Rep Power
    1225
    Code:
                      my ($found1, $found2) = (-1, -1);
                      
                      while (my $line = <$inf_fh>)  {                    
                               
                       if ($line =~/$parama_one/)   {
                           print $line;
                           print $out_fh $line;                 
                       }
                       else
                       {
                            ++$found1;
                            print $out_fh "$parama_one NOT FOUND\n" unless $found1;
                       }
    
                       if ($line =~/$parama_two/)   {
                           print $line ;
                           print $out_fh $line ;                 
                       
                       }
                       else  
                       {
                            ++$found2;
                            print $out_fh "$parama_two NOT FOUND\n" unless $found2;
                       }
              }
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    82
    Rep Power
    3
    Yes, English is not my first language, it is my fourth language.
    I’m working on the fifth one-Perl
    And yes, I tried to use “last” and the code breaks out of the loop.

    As usual, Fish writes magic code and it works perfectly, by adding "my ($found1, $found2) = (-1, -1)" to the code solves my problem.
    I have never seen this kind of expression before.
    Fish, could you elaborate (-1, -1) part if you’ll have time? Or just let me (I’m sure other will be interested too) know where I can read about it?

    Thank you Scorpions4ever, Laurent and Fish again!
    It is really nice of you guys to help people you have never met before.
    Thank you! God bless you!
    TesterV
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    82
    Rep Power
    3
    I probably do something wrong, I’ll try to explain what I need.
    File example:
    Code:
    1111
    aaaa
    2222
    bbbb
    3333
    cccc
    4444
    dddd
    5555
    eeee
    6666
    ffff
    I’m looking for $parama_3, which is ‘zzzz’ in the file that has 12 lines.
    Using while loop it prints “NOT found” for each line that does not match ‘$parama_3’ -12 times.

    I’d like to print “$parama_3 NOT found“ one time.

    Now it prints:
    Code:
    1111 NOT FOUND
    3333 NOT FOUND
    zzzz NOT FOUND
    1111
    3333
    Wanted output:
    Code:
    zzzz NOT FOUND
    1111
    3333
    Code:
        my $parama_one = '1111' ; 
        my $parama_two = '3333' ; 
        my $parama_3   = 'zzzz' ;
    
        my ($found1, $found2) = (-1, -1);
        my $found3 = (-1) ; #not sure if I should about this line#
    
                       while (my $line = <$inf_fh>)  {                    
                               
                       if ($line =~/$parama_one/)   {
                           print $line;
                           print $out_fh $line;                 
                       }
                       else
                       {
                            ++$found1;
                            print "$parama_one NOT FOUND\n" unless $found1;
                            print $out_fh "$parama_one NOT FOUND\n" unless $found1;
                       }
    
                       if ($line =~/$parama_two/)   {
                           print $line ;
                           print $out_fh $line ;                 
                       
                       }
                       else  
                       {
                            ++$found2;
                            print "$parama_two NOT FOUND\n" unless $found2;
                            print $out_fh "$parama_two NOT FOUND\n" unless $found2;
                       }
    
                       if ($line =~/$parama_3/)   {
                           print $line ;
                           print $out_fh $line ;                 
                       
                       }
                       else  
                       {
                            ++$found3;
                            print "$parama_3 NOT FOUND\n" unless $found3;
                            print $out_fh "$parama_3 NOT FOUND\n" unless $found3;
                       }                   
                       
              }
       exit ;
    It is very hard to find black cat in a dark room, especially if it not there.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    831
    Rep Power
    496
    If I understand c orrectly what you are trying to do, I think the approach is flawed.

    This is what I would do:
    - set three flags to 0 (false), one for each pattern
    - Read the file line by line
    - if I find one of my patterns, print the line and set a flag to true for this pattern
    - Once the file has been completely read, print the patterns that are still set to false.

    I don't have time now yo give the full code, so I do it only for one pattern. This gives something like this:

    Perl Code:
              my $flag_one = 0;
              while (my $line = <$inf_fh>)  {                    
     
                       if ($line =~/$parama_one/)   {
                           print $line;
                           print $out_fh $line; 
                           $flag_one = 1 ;            
                       }
                       # same thing for param 2 and 3
               }
                print "$parama_one NOT FOUND" unless $flag_one;
                # same thing for the two other patterns


    Now, to tell the truth, for 3 patterns and 3 flags, and even more so if there is any chance that you have more that 3 patterns, I would use arrays of pattern and arrays of flags ti manage this.
    Last edited by Laurent_R; December 9th, 2012 at 03:05 AM.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    82
    Rep Power
    3
    Thanks Laurent, the code works now!
    I have never thought "about array of patterns' that is a good idea.
    Thanks again man!
    testerV

IMN logo majestic logo threadwatch logo seochat tools logo