ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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";
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 ?
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:
? 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 ?
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:
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.
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.
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:
read 'perldoc -f warn'
comment out 'print $f;'
in the very beginning of the loop body place: 'warn "|\$f|=|$f|";'
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?).
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?).
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.