Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

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;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://982054]
holli just tried to open a soda bottle with a bottleopener
[holli]: ok, that doesn't *sound* that bad
[Corion]: holli: Was it a "twist-to-open" soda bottle? Or a German Mehrweg bottle with a twisty cap?

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2017-09-25 12:32 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (280 votes). Check out past polls.