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;
|
---|
In Section
Seekers of Perl Wisdom