Hi,
since Perl 5.12 you can use Each to give back the index as well:
Code:
use v5.12;
my @ar = ('mais', 'kirschen', 'bohnen', 'gurken');
while(my ($index, $value) = each(@ar)) {
say "Index: $index -> Value: $value";
}
Outcome:
Index: 0 -> Value: mais
Index: 1 -> Value:
On Thu, Aug 08, 2013 at 11:30:29PM -0500, Andy Bach wrote:
> > And buggy, consider:
>my @timings = ( 11, 22, 3, 14, 18, 45, 18, ... 86 );
>
> Yeah, it's a constraint without a cause. Do you want to treat every "18" in
> the "if " or only the first? Why not use a counter? Is the data from a l
On Thu, Aug 08, 2013 at 12:30:10PM -0500, Andy Bach wrote:
> On Thu, Aug 8, 2013 at 12:05 PM, Unknown User
> wrote:
>
> > at any point is it possible to say which element i am handling without
> > using a counter?
>
>
> Er, well, if it were an array rather than a list
> my @letters = (a .. z);
That is true.. Perhaps it's better to introduce a bare block enclosing the
loop, and declare $count as 'my' just before 'foreach'.
Cheers,
Jing
On 9 Aug 2013, at 16:39, Uri Guttman wrote:
> On 08/09/2013 04:34 AM, Jing Yu wrote:
>> You probably can use 'state' instead of 'my' to keep $counter
On 08/09/2013 04:34 AM, Jing Yu wrote:
You probably can use 'state' instead of 'my' to keep $counter in scope.
foreach my $e ( 'a'..'z' ) {
state $counter++;
if ( $counter == 5 ) {
say $e;
}
}
and what if that code is run again in the same program? it will keep the
las
On 08/09/2013 04:24 AM, Dermot wrote:
my $counter = 0;
foreach my $e ( a .. z ) {
$counter++;
if ( $counter == 5 ) {
}
}
I know this is a perl idiom but I, and I suspect others, would find a perl
variable useful for the keeping the count when iterating. The
You probably can use 'state' instead of 'my' to keep $counter in scope.
foreach my $e ( 'a'..'z' ) {
state $counter++;
if ( $counter == 5 ) {
say $e;
}
}
Cheers,
Jing
On 9 Aug 2013, at 16:24, Dermot wrote:
> my $counter = 0;
> foreach my $e ( a .. z ) {
> $counter++;
my $counter = 0;
foreach my $e ( a .. z ) {
$counter++;
if ( $counter == 5 ) {
}
}
I know this is a perl idiom but I, and I suspect others, would find a perl
variable useful for the keeping the count when iterating. The draw back
with the above is that $counter ha
> And buggy, consider:
my @timings = ( 11, 22, 3, 14, 18, 45, 18, ... 86 );
Yeah, it's a constraint without a cause. Do you want to treat every "18" in the
"if " or only the first? Why not use a counter? Is the data from a list, a
file or … ? Do we know it's the 5th element ahead of time?
B
Something like this:
while(){
if(/d/){
print;
say $.;
}
}
__DATA__
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
Cheers,
Jing
On 9 Aug 2013, at 01:05, Unknown User wrote:
>
> Hello,
>
>
> If i am iterating through the elements in an array, at any point is it
>
Or maybe you can convert your list into a file, and use the line number
variable to do what you want.
Cheers,
Jing
On 9 Aug 2013, at 01:05, Unknown User wrote:
>
> Hello,
>
>
> If i am iterating through the elements in an array, at any point is it
> possible to say which element i am handli
On Thu, Aug 8, 2013 at 2:05 PM, Unknown User wrote:
>
> Hello,
>
>
> If i am iterating through the elements in an array, at any point is it
> possible to say which element i am handling without using a counter? Are
> there any builtins that i can use for it?
>
> ie
> foreach my $element (a..z) {
>
On Thu, Aug 8, 2013 at 12:05 PM, Unknown User wrote:
> at any point is it possible to say which element i am handling without
> using a counter?
Er, well, if it were an array rather than a list
my @letters = (a .. z);
foreach my $letter ( a .. z ) {
if ( $letter eq $letters[4] ) {
but that's
my $counter = 0;
foreach my $e ( a .. z ) {
$counter++;
if ( $counter == 5 ) {
}
}
On Thu, Aug 8, 2013 at 12:11 PM, jbiskofski wrote:
> my $counter = 0;
> foreach my $e ( a .. z ) {
> $counter++;
>
>
>
> On Thu, Aug 8, 2013 at 12:05 PM, Unknown User
> wr
my $counter = 0;
foreach my $e ( a .. z ) {
$counter++;
On Thu, Aug 8, 2013 at 12:05 PM, Unknown User wrote:
>
> Hello,
>
>
> If i am iterating through the elements in an array, at any point is it
> possible to say which element i am handling without using a counter? Are
> there any built
Hello,
If i am iterating through the elements in an array, at any point is it
possible to say which element i am handling without using a counter? Are
there any builtins that i can use for it?
ie
foreach my $element (a..z) {
...
if ( i am the 5th element ) { handle me special }
}
Thanks,
16 matches
Mail list logo