Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Counter in foreach loop

by RustyShackelford (Initiate)
on Dec 05, 2009 at 22:38 UTC ( #811275=perlquestion: print w/replies, xml ) Need Help??
RustyShackelford has asked for the wisdom of the Perl Monks concerning the following question:

My counter won't increment and I dont know why, here is my code:
#!/usr/bin/perl use strict; my $count=0; my @cp_op = "111 222 333"; foreach my $line (@cp_op) #splits each line of the array { print "$line\n"; $count ++; } print "$count\n";
The code outputs:
111 222 333 1
Why does count only increment once instead of 3 times, once for each line??? Please help

20091208 Janitored by Corion: Added formatting, code tags, as per Writeup Formatting Tips

Replies are listed 'Best First'.
Re: Counter in foreach loop
by jwkrahn (Monsignor) on Dec 05, 2009 at 22:47 UTC

    Your array contains a single scalar string, not three lines.    You want something like this instead:

    my @cp_op = ( "111", "222", "333" );
Re: Counter in foreach loop
by AnomalousMonk (Chancellor) on Dec 05, 2009 at 23:18 UTC

    Further to jwkrahn's reply:
    You can find the number of elements in an array by evaluating the array in scalar context (also note that printing an array 'element' demarcated by something like single-quotes in a piece of debug code like this shows each element more clearly):

    >perl -wMstrict -le "my $count=0; my @cp_op = '111 222 333'; print 'elements in array: ', scalar @cp_op; foreach my $line (@cp_op) { print qq{'$line'}; $count ++; } print qq{$count}; " elements in array: 1 '111 222 333' 1

    Also: Please update your original post to wrap your code in  <code> ... </code> or  <c> ... </c> tags. Please see Markup in the Monastery.

Re: Counter in foreach loop
by biohisham (Priest) on Dec 06, 2009 at 01:21 UTC
    You might be interested in Quote Like Operators. Here's your code again but more intelligible because of it being of proper indentation and within the appropriate tags <code>Here is the place you put your code in </code>:
    use strict; my $count=0; my @cp_op = qw(111 222 333); foreach my $line (@cp_op) { #splits each line of the array print "$line\n"; $count ++; } print "$count\n";

    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.
Re: Counter in foreach loop
by Bloodnok (Vicar) on Dec 06, 2009 at 00:54 UTC
    On a point of pedantry &/or etiquette, may I be the first to point out that your code should've been enclosed in <c> ... </c> tags (as it says in emboldened font just below the text box used to enter your posting) - the use of those tags causes the code to be rendered clearly (c/w the line noise in your post) as ably demonstrated by the code blocks in the posts of previous 2, and most other, monks.


    Thanx to AnomalousMonk for spotting out the deliberate, now corrected mistake wrt the closing tag.

    A user level that continues to overstate my experience :-))

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://811275]
Approved by AnomalousMonk
and the daffodils sway...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2017-07-22 07:39 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (337 votes). Check out past polls.