LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-19-2010, 03:22 AM   #1
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Rep: Reputation: 32
Get me out of this while loop( perl regexp)


Guys,
Get me out of the while loop I am using in the code which is:
Code:
#find the no. of occurences of a word in the given line
$a=<STDIN>;
$f=$a;
while($f ne "")
{
	if($f=~/(hi)/)
	{
		#print $f;
		$c++;
	}
$f=$';
}
print "The total no. of ocurrances are $c\n";
Thanks in advance

Last edited by ashok.g; 02-19-2010 at 06:10 AM.
 
Old 02-19-2010, 03:53 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by ashok.g View Post
Guys,
Get me out of the while loop I am using in the code which is:
Code:
$a=<STDIN>;
$f=$a;
while((defined $f) != " ")
{
	if($f=~/(hi)/)
	{
		print $f;
		$c++;
	}
$f=$';

}
Thanks in advance
And why do you think you "deserve" getting out of the loop ? I.e. what diagnostic information do you have to prove that conditions exist to get out of the loop ?

...

For starters, have you actually thought about the validity of this:

Code:
(defined $f) != " "
comparison ? I.e. about types/values of left and right hand sides ?
 
Old 02-19-2010, 04:00 AM   #3
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Original Poster
Rep: Reputation: 32
Sergei Steshenko,
I don't really get you what you are trying to say!!
 
Old 02-19-2010, 04:14 AM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by ashok.g View Post
Sergei Steshenko,
I don't really get you what you are trying to say!!
Do you compare:
  1. apples to apples ?
  2. apples to oragnes ?
.
After answering the above question try again to answer the question I've asked:

Quote:
For starters, have you actually thought about the validity of this:

Code:
 (defined $f) != " "
comparison ? I.e. about types/values of left and right hand sides ?
 
Old 02-19-2010, 06:09 AM   #5
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Original Poster
Rep: Reputation: 32
Quote:
Originally Posted by Sergei Steshenko View Post
After answering the above question try again to answer the question I've asked:
Ok, I made the changes to the code.... and edited in the main thread. Now tell me wheres my code went wrong????
 
Old 02-19-2010, 06:22 AM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by ashok.g View Post
Ok, I made the changes to the code.... and edited in the main thread. Now tell me wheres my code went wrong????
And why should I or anybody else tell you what went wrong ?

The thing that is wrong from the very beginning is lack of debugging skills.

What you are doing is conceptually this:

Code:
while(<condition>)
  {
  <do_something_affecting_the_condition>
  }
You expect <condition> to become false sooner or later, and thus exit the loop, but it apparently it doesn't happen.

So, why should anybody guess why <condition> doesn't go false ? Why don't you make the effort yourself and and print the condition ? I.e. why don't you change your code to become, say:

Code:
while(<condition>)
  {
  <print_the_condition_and_its_internals>
  <do_something_affecting_the_condition>
  }
and/or, if, for example, you definitely know that the loop body shouldn't be executed more than 5 times, then:

Code:
for(my $attempt_number = 0; $attempt_number < 5; $attempt_number++)
#while(<condition>)
  {
  <print_the_condition_and_its_internals>
  <do_something_affecting_the_condition>
  }
?

Then analyze what is printed and fix the problem.
 
Old 02-19-2010, 06:25 AM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
And why don't you use

Code:
use strict;
use warnings;
? I.e. what makes you think your Perl skills are so good you do not need compiler diagnostics ? And what makes you think implementing everything through global (rather than lexical) variables is a good idea ?
 
Old 02-19-2010, 06:35 AM   #8
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Original Poster
Rep: Reputation: 32
Quote:
Originally Posted by Sergei Steshenko View Post
And why should I or anybody else tell you what went wrong ?
I think this forum is for sharing the views, helping others in getting out of their problems(technical n non-technical if possible),etc. Isn't it?
Quote:
Originally Posted by Sergei Steshenko View Post
The thing that is wrong from the very beginning is lack of debugging skills.
Who said I didn't used any debugging skills?
For your kind information here is the code where I used some debugging skills
Code:
#print $f;
Quote:
Originally Posted by Sergei Steshenko View Post
What you are doing is conceptually this:

Code:
while(<condition>)
  {
  <do_something_affecting_the_condition>
  }
You expect <condition> to become false sooner or later, and thus exit the loop, but it apparently it doesn't happen.

So, why should anybody guess why <condition> doesn't go false ? Why don't you make the effort yourself and and print the condition ? I.e. why don't you change your code to become, say:

Code:
while(<condition>)
  {
  <print_the_condition_and_its_internals>
  <do_something_affecting_the_condition>
  }
and/or, if, for example, you definitely know that the loop body shouldn't be executed more than 5 times, then:

Code:
for(my $attempt_number = 0; $attempt_number < 5; $attempt_number++)
#while(<condition>)
  {
  <print_the_condition_and_its_internals>
  <do_something_affecting_the_condition>
  }
?

Then analyze what is printed and fix the problem.
I think its better to solve the problem rather than teaching the entire control statements like while, for,etc., which I had already know if you can.
 
Old 02-19-2010, 06:41 AM   #9
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by ashok.g View Post
I think this forum is for sharing the views, helping others in getting out of their problems(technical n non-technical if possible),etc. Isn't it?

Who said I didn't used any debugging skills?
For your kind information here is the code where I used some debugging skills
Code:
#print $f;
I think its better to solve the problem rather than teaching the entire control statements like while, for,etc., which I had already know if you can.
I do not see the printouts. And I do now want to guess. And I do not know what your input data is.

Every bug reporting guidelines demand publishing log files/screen output when they are available. In your case screen output can definitely be made available.
 
Old 02-19-2010, 06:54 AM   #10
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Original Poster
Rep: Reputation: 32
Quote:
Originally Posted by Sergei Steshenko View Post
I do not see the printouts. And I do now want to guess. And I do not know what your input data is.

Every bug reporting guidelines demand publishing log files/screen output when they are available. In your case screen output can definitely be made available.
Here you go!
I am presenting in front of you the entire input/output and source files.
Firstly the source file:
Code:
#find the no. of occurences of a word in the given line
$a=<STDIN>;
$f=$a;
while($f ne "")
{
	if($f=~/(hi)/)
	{
		print $f;
		$c++;
	}
$f=$';
}
print "The total no. of ocurrances are $c\n";
Input/Output:
Code:
[Ashok@station130 My Work]$ perl temp.pl
hi hhhhi hihihi hihi
hi hhhhi hihihi hihi
 hhhhi hihihi hihi
 hihihi hihi
hihi hihi
hi hihi
 hihi
hi
Observe that in the end I am not able to return to my prompt again.Its running an infinite loop there.
Now I think this information is more than enough for you and its your turn now.
 
Old 02-19-2010, 07:03 AM   #11
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by ashok.g View Post
Here you go!
I am presenting in front of you the entire input/output and source files.
Firstly the source file:
Code:
#find the no. of occurences of a word in the given line
$a=<STDIN>;
$f=$a;
while($f ne "")
{
	if($f=~/(hi)/)
	{
		print $f;
		$c++;
	}
$f=$';
}
print "The total no. of ocurrances are $c\n";
Input/Output:
Code:
[Ashok@station130 My Work]$ perl temp.pl
hi hhhhi hihihi hihi
hi hhhhi hihihi hihi
 hhhhi hihihi hihi
 hihihi hihi
hihi hihi
hi hihi
 hihi
hi
Observe that in the end I am not able to return to my prompt again.Its running an infinite loop there.
Now I think this information is more than enough for you and its your turn now.
No, the information is not sufficient (but I think I know what the problem is).

As I said, the problem is debug skills, and, in this case, poor way to display a variable with text contents.

The point is that text can also contain whitespaces, and you made no effort to implement your print statements in a manner than shows whitespaces.

So, for starters, assuming that your input data does not contain '|' character, please do the following:
  1. read 'perldoc -f warn'
  2. comment out 'print $f;'
  3. in the very beginning of the loop body place: 'warn "|\$f|=|$f|";'

and publish the screen output again.
 
Old 02-19-2010, 07:13 AM   #12
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

Here's a solution for your problem, based on your original code:
Code:
#!/usr/bin/perl

use strict ;
use warnings ;

my $a ;
my $c = '0';
my $f ;

$a = <STDIN> ;
$f = $a ;
while ( $f ne "" ) {
   chomp $f;
   if ( $f =~ /(hi)/ ) {
      $c++ ;
   }
   $f = $' ;
}
print "The total no. of ocurrances are $c\n" ;

exit 0;
I agree with Sergei Steshenko that you do need to give the appropriate information (which you partially did).

It is up to you to figure out why the above code works and yours did not (you do want to learn something, don't you?).

Anyway, hope this helps.
 
Old 02-19-2010, 07:16 AM   #13
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by druuna View Post
Hi,

Here's a solution for your problem, based on your original code:
Code:
#!/usr/bin/perl

use strict ;
use warnings ;

my $a ;
my $c = '0';
my $f ;

$a = <STDIN> ;
$f = $a ;
while ( $f ne "" ) {
   chomp $f;
   if ( $f =~ /(hi)/ ) {
      $c++ ;
   }
   $f = $' ;
}
print "The total no. of ocurrances are $c\n" ;

exit 0;
I agree with Sergei Steshenko that you do need to give the appropriate information (which you partially did).

It is up to you to figure out why the above code works and yours did not (you do want to learn something, don't you?).

Anyway, hope this helps.

Arrrrgh, you spoiled such a nice party .
 
Old 02-19-2010, 07:22 AM   #14
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
@Sergei Steshenko: LOL.

I do hope that the OP doesn't just copy/paste that line, but understands why this needs to be implemented this way. But like I stated before, that is up to him. I also hope that it is clear to the OP why strict/warnings are needed in the code.
 
Old 02-19-2010, 07:36 AM   #15
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Original Poster
Rep: Reputation: 32
I am really enjoying this guys.

I think your code really works for only some inputs.
But what about this input?
Code:
[Ashok@station130 My Work]$ perl temp.pl
not getting hi here
Its again running an infinite loop for YOUR code. You debug your code now.

Last edited by ashok.g; 02-19-2010 at 07:40 AM.
 
  


Reply

Tags
matching, perl


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regexp: difference between sed and Perl matiasar Programming 2 10-15-2009 11:03 AM
Need a perl regexp master sal_paradise42 Programming 8 10-14-2007 06:17 PM
Perl Regexp search-n-replace jpbarto Programming 2 06-16-2005 12:45 PM
perl simple regexp champ Programming 3 07-07-2004 03:27 AM
perl regexp problem raven Programming 4 03-21-2004 11:49 PM


All times are GMT -5. The time now is 06:03 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration