Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Syntax error in using do-until loop: How can I fix it?

by jwkrahn (Monsignor)
on Jul 16, 2012 at 18:18 UTC ( #982054=note: print w/ replies, xml ) Need Help??


in reply to Syntax error in using do-until loop: How can I fix it?

$sentence=~ s/\s//igs;

The /i and /s options are superfluous.    That should just be s/\s//g or maybe s/\s+//g




my @four=$sentence=~ /[a-zA-Z]{4}/igs; # Line 9

The /i and /s options are superfluous.




my $length=length($word);

Your pattern matches exactly four characters so the length will always be 4.




unless (open(RESULT,">$output")){

You are opening this file inside the loop so only the last word will be saved to the file.




} until ( my $word=~ /^.*$/); # Line 22

$word is created here, at this point, using my so it will always be empty.




How can I fix it?

Something like this:

#!/usr/bin/perl use warnings; use strict; ## To chop a sentence at intervals of 4-letter and to print results: my $sentence = "BEAR CALF DEER FEAR GEAR HEAR"; ## To remove blank spaces $sentence =~ s/\s//g; my $output="Words .txt"; open RESULT, '>', $output or do { print "Cannot open file \"$output\".because: $!"; exit; }; print "\n Words are: \n"; foreach my $word ( $sentence =~ /[a-zA-Z]{4}/g ) { print"\n $word: "; print"\n Length of the word = 4\n\n"; print RESULT "\n Words are: \n Word: $word; Length of the word = 4\n\n"; } close RESULT;


Comment on Re: Syntax error in using do-until loop: How can I fix it?
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://982054]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (10)
As of 2014-07-23 05:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (133 votes), past polls