The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Perl Programming
|
Is possible to “print ones”?
Discuss Is possible to “print ones”? in the Perl Programming forum on Dev Shed. Is possible to “print ones”? Perl Programming forum discussing coding in Perl, utilizing Perl modules, and other Perl-related topics. Perl, the Practical Extraction and Reporting Language, is the choice for many for parsing textual information.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 8th, 2012, 12:46 AM
|
|
Contributing User
|
|
Join Date: Jun 2012
Posts: 65
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
|

December 8th, 2012, 12:50 AM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
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
|

December 8th, 2012, 03:12 AM
|
|
|
|
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.
|

December 8th, 2012, 08:59 AM
|
|
|
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;
}
}
|

December 8th, 2012, 04:52 PM
|
|
Contributing User
|
|
Join Date: Jun 2012
Posts: 65
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
|

December 8th, 2012, 06:11 PM
|
|
Contributing User
|
|
Join Date: Jun 2012
Posts: 65
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.
|

December 9th, 2012, 03:01 AM
|
|
|
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 |
|
|
|
my $flag_one = 0; while (my $line = <$inf_fh>) { if ($line =~/$parama_one/) { $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.
|

December 9th, 2012, 11:59 PM
|
|
Contributing User
|
|
Join Date: Jun 2012
Posts: 65
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
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|