Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 8th, 2012, 12:46 AM
testerV testerV is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 65 testerV User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 h 35 m 42 sec
Reputation Power: 2
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

Reply With Quote
  #2  
Old December 8th, 2012, 12:50 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,406 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 9 h 47 m 28 sec
Reputation Power: 4080
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

Reply With Quote
  #3  
Old December 8th, 2012, 03:12 AM
Laurent_R Laurent_R is online now
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 546 Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 5 Days 2 h 42 m 48 sec
Reputation Power: 406
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.

Reply With Quote
  #4  
Old December 8th, 2012, 08:59 AM
FishMonger FishMonger is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2009
Posts: 1,678 FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 19 h 9 m 28 sec
Reputation Power: 1170
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;
                   }
          }

Reply With Quote
  #5  
Old December 8th, 2012, 04:52 PM
testerV testerV is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 65 testerV User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 h 35 m 42 sec
Reputation Power: 2
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

Reply With Quote
  #6  
Old December 8th, 2012, 06:11 PM
testerV testerV is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 65 testerV User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 h 35 m 42 sec
Reputation Power: 2
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.

Reply With Quote
  #7  
Old December 9th, 2012, 03:01 AM
Laurent_R Laurent_R is online now
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 546 Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 5 Days 2 h 42 m 48 sec
Reputation Power: 406
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:
Original - Perl Code
  1.           my $flag_one = 0;
  2.           while (my $line = <$inf_fh>)  {                   
  3.                            
  4.                    if ($line =~/$parama_one/)   {
  5.                        print $line;
  6.                        print $out_fh $line;
  7.                        $flag_one = 1 ;           
  8.                    }
  9.                    # same thing for param 2 and 3
  10.            }
  11.             print "$parama_one NOT FOUND" unless $flag_one;
  12.             # same thing for the two other patterns
  13.  


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.

Reply With Quote
  #8  
Old December 9th, 2012, 11:59 PM
testerV testerV is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 65 testerV User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 h 35 m 42 sec
Reputation Power: 2
Thanks Laurent, the code works now!
I have never thought "about array of patterns' that is a good idea.
Thanks again man!
testerV

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Is possible to “print ones”?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap