Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

problem with foreach

by jeanluca (Deacon)
on Jun 16, 2006 at 07:23 UTC ( [id://555699]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,

In the following example I try to count the number of bs. And I tried to do all this on one line:
#! /usr/bin/perl use strict ; use warnings ; my @a ; $a[0] = "a" ; $a[1] = "a" ; $a[2] = "b" ; $a[3] = "a" ; my $b = 0 ; $b ++ if ( $_ eq "b" ) foreach @a ; print "$b\n" ;
Can someone explain to me why this is just wrong and if it is not too much to ask, what is the solution ?

Thanks
Luca

Replies are listed 'Best First'.
Re: problem with foreach
by bart (Canon) on Jun 16, 2006 at 07:41 UTC
    You can't stack statement modifiers, thus
    $b ++ if ( $_ eq "b" ) foreach @a ;
    can't possibly work. Unfortunately. See perlsyn:
    Any simple statement may optionally be followed by a SINGLE modifier, just before the terminating semicolon (or block ending).
    Note the stress on "single". That's what it means.

    This will work:

    ( $_ eq "b" ) and $b ++ foreach @a ;
    as will this:
    foreach(@a) { $b ++ if ( $_ eq "b" ); }

    p.s. I think it's not good style to detach the ++ operator from the lvalue it's working on. So, use $b++ instead of $b ++.

    And in the

    statement if condition;
    syntax, the parens around the condition are unnecessary.
      do you mean there is a difference between $b++ and $b ++ ?
        Apparently, there isn't. But I had to test it.

        I just have a very hard time reading and understanding it — while it's just such a small thing. Bring on the huge expressions!

        Your whitespace isn't improving readability — on the contrary.

        You should remember that there _is_ a difference betwixt ++$b and $b++, however. ++$b doesn't have the nuisance of making a copy of its original value to return before incrementing, and therefore it's slightly less efficient if, as in this case, you don't need to know what that value was.
Re: problem with foreach
by McDarren (Abbot) on Jun 16, 2006 at 07:37 UTC
    Similar thing, using a grep..
    #!/usr/bin/perl use strict ; use warnings ; my @foo = qw( a b c b d b e b f ); my $bar = grep { $_ eq 'b' } @foo; print "$bar\n";
Re: problem with foreach
by Zaxo (Archbishop) on Jun 16, 2006 at 07:31 UTC

    strict/warnings will clobber my $b because $a and $b are special variables. Change the name.

    Dead wrong, that first guess. Two modifiers, one statement. Rewrite as,

    $_ eq "b" and $b++ foreach @a ;
    or,
    for (@a) { $b++ if $_ eq "b"; }

    After Compline,
    Zaxo

Re: problem with foreach
by Jasper (Chaplain) on Jun 16, 2006 at 08:55 UTC
    $b += $_ eq 'b' for @a; It'll work until they change true.
Re: problem with foreach
by neniro (Priest) on Jun 16, 2006 at 07:39 UTC
    EDIT: I'm to slow - see node above

    You can fill the array in one step using qw, and you can count all 'b' in this array in one step using grep:

    my @ar = qw /a b b a/; my $cnt = 0 ; $cnt = grep /b/, @ar;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-20 13:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found