Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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

by supriyoch_2008 (Monk)
on Jul 16, 2012 at 10:12 UTC ( [id://981995]=perlquestion: print w/replies, xml ) Need Help??

supriyoch_2008 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Monks,

I am a beginner in perl script writing. I have written a small script word.pl (given below) using a do-until loop to chop a sentence in 4-letter words. But it is not showing the results. The line number 22 shows a syntax error for ");". I have not been able to correct the error. I have written line number 22 to search the sentence fron beginning to the end for every 4-letter word. May I expect suggestions from perl monks regarding the error? The script goes like:

#!/usr/bin/perl-w ## To chop a sentence at intervals of 4-letter and to print results: use strict; my $sentence="BEAR CALF DEER FEAR GEAR HEAR"; ## To remove blank spaces: # Line 5 $sentence=~ s/\s//igs; print"\n Words are: \n"; do { my @four=$sentence=~ /[a-zA-Z]{4}/igs; # Line 9 foreach my $word(@four) {print"\n $word: "; my $length=length($word); print"\n Length of the word= $length\n\n"; my $output="Words .txt"; # Line 14 unless (open(RESULT,">$output")){ print"Cannot open file\"$output\".\n\n"; # Line 16 exit; } print RESULT"\n Words are: \n Word: $word; Length of the word= $length\n\n"; close(RESULT); # Line 21 } until ( my $word=~ /^.*$/); # Line 22 exit; }

I have got the following results in cmd screen:

Microsoft Windows [Version 6.1.7600] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\xxxxxxx\Desktop>word.pl syntax error at C:\Users\xxxxxx\Desktop\word.pl line 22, near ");" Execution of C:\Users\xxxxxxxx\Desktop\word.pl aborted due to compilat +ion errors.

Replies are listed 'Best First'.
Re: Syntax error in using do-until loop: How can I fix it?
by BrowserUk (Patriarch) on Jul 16, 2012 at 10:21 UTC

    If you formatted your code correctly, the reason becomes obvious.

    You've added your until clause to the end of your foreach loop:

    #!/usr/bin/perl-w ## To chop a sentence at intervals of 4-letter and to print results: use strict; my $sentence="BEAR CALF DEER FEAR GEAR HEAR"; ## To remove blank spaces: # Line 5 $sentence=~ s/\s//igs; print"\n Words are: \n"; do { my @four=$sentence=~ /[a-zA-Z]{4}/igs; # Line 9 foreach my $word(@four) { print"\n $word: "; my $length=length($word); print"\n Length of the word= $length\n\n"; my $output="Words .txt"; # Line 14 unless (open(RESULT,">$output")){ print"Cannot open file\"$output\".\n\n"; # Line 16 exit; } print RESULT"\n Words are: \n Word: $word; Length of the word= $length\n\n"; close(RESULT); # Line 21 } until ( my $word=~ /^.*$/); # Line 22 exit; }

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

Re: Syntax error in using do-until loop: How can I fix it?
by aitap (Curate) on Jul 16, 2012 at 10:22 UTC

    The opening vurly bracket ({) that matches the closing one (}) on line 22 is placed on line 11, so your code between these lines looks like this: { some; code; } (while my $word =~ /^.*$/)

    I guess that you missed closing curly bracket (}) after line 13:

    foreach my $word(@four) {print"\n $word: "; my $length=length($word); print"\n Length of the word= $length\n\n"; } # there
    and the one on line 24 is not needed.

    UPD: you can split words easier using split:

    #!/usr/bin/perl -w use strict; use Data::Dumper; my $sentence="BEARCALFDEERFEARGEARHEAR"; print Dumper (split /(.{4})/,$sentence); __END__ $VAR1 = ''; $VAR2 = 'BEAR'; $VAR3 = ''; $VAR4 = 'CALF'; $VAR5 = ''; $VAR6 = 'DEER'; $VAR7 = ''; $VAR8 = 'FEAR'; $VAR9 = ''; $VAR10 = 'GEAR'; $VAR11 = ''; $VAR12 = 'HEAR';

    Sorry if my advice was wrong.
Re: Syntax error in using do-until loop: How can I fix it?
by grumbert (Scribe) on Jul 16, 2012 at 10:24 UTC
    From a quick glance it looks like you are missing a } before the until. The } you have before until closes the foreach loop rather than the do {} . The } after exit is one too much.

    I would strongly recommend using a proper indentation/bracket style, it tends to make reading and debugging code a bit easier :)

Re: Syntax error in using do-until loop: How can I fix it?
by jwkrahn (Abbot) on Jul 16, 2012 at 18:18 UTC
    $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;
Re: Syntax error in using do-until loop: How can I fix it?
by BillKSmith (Monsignor) on Jul 16, 2012 at 19:00 UTC
Re: Syntax error in using do-until loop: How can I fix it?
by pvaldes (Chaplain) on Jul 16, 2012 at 23:25 UTC

    Wishful thinking must die...

    my $sentence="to baah or not to chomp, that's the question"; my $garbage = 0; $garbage++ while ($sentence =~ /[,'\s]/g); @chars = split //, $sentence; print "You can create ", (scalar(@chars) - $garbage)/4.0," groups of f +our characters here\n";

    A little twisted but it works... note that you don't lose any info here in your sentence

    Updated: printing the groups

    @chars = grep {!/(,|'|\s)/} @chars; my $n = 0; while ($n < $#chars){ print $chars[$n],$chars[$n+1],$chars[$n+2],$chars[$n+3],"\n"; $n = $n+4}

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://981995]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-03-19 08:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found