Discussion:
last element during foreach loop
six24hourdays
2008-10-21 13:39:05 UTC
Permalink
Hello,

I would like to know how to test for the last element of a list during
a foreach loop, e.g.

foreach $element (@List) {
if (this is the last element) {
do something
}
}

What would I use for the IF condition?

Thanks,
Dave
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
http://learn.perl.org/
Jeff Pang
2008-10-21 14:09:35 UTC
Permalink
Post by six24hourdays
Hello,
I would like to know how to test for the last element of a list during
a foreach loop, e.g.
if (this is the last element) {
do something
}
}
my $i =0;
for (@list) {
if ($i == $#list) {
....
}
$i ++;
}
--
Jeff Pang
http://home.arcor.de/pangj/
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
http://learn.perl.org/
Chas. Owens
2008-10-21 14:44:23 UTC
Permalink
Post by six24hourdays
Hello,
I would like to know how to test for the last element of a list during
a foreach loop, e.g.
if (this is the last element) {
do something
}
}
What would I use for the IF condition?
snip

This is an odd desire; are you sure you really need to do this? Can
you tell us what you are trying to achieve? Jeff's answer does what
you want, but most likely there is a better approach to be using for
your actual problem.
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
http://learn.perl.org/
Jeff Pang
2008-10-21 14:47:38 UTC
Permalink
Post by Chas. Owens
This is an odd desire; are you sure you really need to do this? Can
you tell us what you are trying to achieve? Jeff's answer does what
you want, but most likely there is a better approach to be using for
your actual problem.
Agree.
He probably want the last element from the array, then try,
my $last = $list[-1];
--
my @name = glob "{JeffP}{a,e}{ng}"
http://home.arcor.de/pangj/
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
http://learn.perl.org/
Mr. Shawn H. Corey
2008-10-21 14:55:48 UTC
Permalink
Post by Jeff Pang
Post by Chas. Owens
This is an odd desire; are you sure you really need to do this? Can
you tell us what you are trying to achieve? Jeff's answer does what
you want, but most likely there is a better approach to be using for
your actual problem.
Agree.
He probably want the last element from the array, then try,
my $last = $list[-1];
if( @list ){
my $last_item = pop @list;
for my $item ( @list ){
# do stuff for all items but last
}
# do stuff for $last_item
}
--
Just my 0.00000002 million dollars worth,
Shawn

Linux is obsolete.
-- Andrew Tanenbaum
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
http://learn.perl.org/
Mr. Shawn H. Corey
2008-10-21 15:04:28 UTC
Permalink
Post by Mr. Shawn H. Corey
# do stuff for all items but last
}
# do stuff for $last_item
}
If you want @list restored after you're done processing:

if( @list ){
my $last_item = pop @list;
for my $item ( @list ){
# do stuff for all items but last
}
# do stuff for $last_item
push @list, $last_item;
}
--
Just my 0.00000002 million dollars worth,
Shawn

Linux is obsolete.
-- Andrew Tanenbaum
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
http://learn.perl.org/
Chas. Owens
2008-10-21 15:06:59 UTC
Permalink
Hi Chas,
There probably is a better approach.
I am going through a list of Subversion branch names, e.g.
BRANCH_1
BRANCH_1
BRANCH_2
BRANCH_2
BRANCH_3
BRANCH_3
BRANCH_4
BRANCH_4
and checking to see if $branch eq $lastBranch. Then I do something.
The problem is that the last time through the loop, e.g. BRANCH_4,
nothing happens because the IF never evaluates to true.
If I understand you correctly then this code should work for you:

#!/usr/bin/perl

use strict;
use warnings;

#open my $fh, "-|", "svn somehing"
# or die "could not run 'svn something': $!";

my $old_branch = '';
while (my $new_branch = <DATA>) { #use $fh here instead of DATA
chomp $new_branch;
if ($old_branch eq $new_branch) {
print "doing stuff to $new_branch\n";
}
$old_branch = $new_branch;
}

__DATA__
BRANCH_1
BRANCH_1
BRANCH_2
BRANCH_2
BRANCH_3
BRANCH_3
BRANCH_4
BRANCH_4
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
http://learn.perl.org/
Chas. Owens
2008-10-21 15:11:21 UTC
Permalink
On Tue, Oct 21, 2008 at 11:06, Chas. Owens <***@gmail.com> wrote:
snip
Post by Chas. Owens
#open my $fh, "-|", "svn somehing"
# or die "could not run 'svn something': $!";
snip

Whoops, that is what I get for going too fast. The multiple argument
version is faster because it doesn't need to spawn a sub shell:

open my $fh, "-|", "svn", "arg1", "arg2", "etc"
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
http://learn.perl.org/
six24hourdays
2008-10-21 18:01:19 UTC
Permalink
Post by Chas. Owens
Hi Chas,
There probably is a better approach.
I am going through a list of Subversion branch names, e.g.
BRANCH_1
BRANCH_1
BRANCH_2
BRANCH_2
BRANCH_3
BRANCH_3
BRANCH_4
BRANCH_4
and checking to see if $branch eq $lastBranch. Then I do something.
The problem is that the last time through the loop, e.g. BRANCH_4,
nothing happens because the IF never evaluates to true.
#!/usr/bin/perl
use strict;
use warnings;
#open my $fh, "-|", "svn somehing"
#       or die "could not run 'svn something': $!";
my $old_branch = '';
while (my $new_branch = <DATA>) { #use $fh here instead of DATA
        chomp $new_branch;
        if ($old_branch eq $new_branch) {
                print "doing stuff to $new_branch\n";
        }
        $old_branch = $new_branch;
}
__DATA__
BRANCH_1
BRANCH_1
BRANCH_2
BRANCH_2
BRANCH_3
BRANCH_3
BRANCH_4
BRANCH_4
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
This works if there are 2 of each branch (as in the example). If there
are more then the IF condition is true more times than I want. I only
want a condition to be true for the transition to a new branch. Then I
email the branch owner with the list.

Right now I am just pushing a dummy entry onto the end of the list so
my IF condition (if $branch ne $lastBranch) is true at the transition.

Dave
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
http://learn.perl.org/
Rob Dixon
2008-10-21 22:38:57 UTC
Permalink
Post by six24hourdays
Post by Chas. Owens
There probably is a better approach.
I am going through a list of Subversion branch names, e.g.
BRANCH_1
BRANCH_1
BRANCH_2
BRANCH_2
BRANCH_3
BRANCH_3
BRANCH_4
BRANCH_4
and checking to see if $branch eq $lastBranch. Then I do something.
The problem is that the last time through the loop, e.g. BRANCH_4,
nothing happens because the IF never evaluates to true.
#!/usr/bin/perl
use strict;
use warnings;
#open my $fh, "-|", "svn somehing"
# or die "could not run 'svn something': $!";
my $old_branch = '';
while (my $new_branch = <DATA>) { #use $fh here instead of DATA
chomp $new_branch;
if ($old_branch eq $new_branch) {
print "doing stuff to $new_branch\n";
}
$old_branch = $new_branch;
}
__DATA__
BRANCH_1
BRANCH_1
BRANCH_2
BRANCH_2
BRANCH_3
BRANCH_3
BRANCH_4
BRANCH_4
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
This works if there are 2 of each branch (as in the example). If there
are more then the IF condition is true more times than I want. I only
want a condition to be true for the transition to a new branch. Then I
email the branch owner with the list.
Right now I am just pushing a dummy entry onto the end of the list so
my IF condition (if $branch ne $lastBranch) is true at the transition.
I'm sure what Chas mean was this

if ($old_branch ne $new_branch) {
print "doing stuff to $new_branch\n";
}

which works fine.

If you prefer you could use a hash, which is the classical way to determine
whether a data item has been seen already. Use a loop like this.

my %branches;
while (my $name = <DATA>) {
chomp $name;
next if $branches{$name}++;

print "doing stuff to $name\n";
}

HTH,

Rob
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
http://learn.perl.org/
John W. Krahn
2008-10-21 15:51:54 UTC
Permalink
Post by six24hourdays
Hello,
Hello,
Post by six24hourdays
I would like to know how to test for the last element of a list during
a foreach loop, e.g.
if (this is the last element) {
do something
}
}
What would I use for the IF condition?
You can't because a list does not have a name.

perldoc -q "What is the difference between a list and an array"


You can however do it with an array by comparing references:

$ perl -le'
my @x = "a" .. "z";
for my $elem ( @x ) {
print $elem if \$elem == \$x[-1];
}
'
z



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
http://learn.perl.org/
Dr.Ruud
2008-10-21 21:35:07 UTC
Permalink
Post by six24hourdays
I would like to know how to test for the last element of a list
during a foreach loop [...]
You can't because a list does not have a name. [...]
$ perl -le'
print $elem if \$elem == \$x[-1];
}
'
z
There are of course ways in Perl to make this fail. One way:

$ perl -MData::Alias -wle'
my @x = "a".."z";
alias $x[3] = $x[-1];
$x[-1] = "test";
for my $elem (@x) {
print $elem if \$elem == \$x[-1];
}
'
test
test
--
Affijn, Ruud

"Gewoon is een tijger."
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
http://learn.perl.org/
Vijaya
2008-10-22 08:36:46 UTC
Permalink
Hi Dave,

I think there are 3 possible ways to check the last element in your IF
condition inside Foreach loop.

1. $list[-1] eq $element
2. $list[$#list] eq $element
3. $list[scalar(@list)-1] eq $element

See the example code and replace the if with other set of conditions given
above

@list = qw/a d b y I s n p/;
Foreach $element (@list) {
If ($list[-1] eq $element) {
Print "last element is $element";
}
}

Output : last element is p

Regards,
Vijaya
Dr.Ruud
2008-10-22 08:54:36 UTC
Permalink
Post by Vijaya
I think there are 3 possible ways to check the last element in your IF
condition inside Foreach loop.
1. $list[-1] eq $element
2. $list[$#list] eq $element
Those all will not work (reliably), because you compare the value, and
the values of an array are not necessarily unique.
--
Affijn, Ruud

"Gewoon is een tijger."
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
http://learn.perl.org/
Loading...