473,387 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to tell the last iteration in a foreach loop?

Expand|Select|Wrap|Line Numbers
  1. foreach $key (keys (%hash)){
  2.   print "$hash{$key}:";
  3. }
  4.  
The last element will also be followed by a ":" which is not what I want. Is there a way to tell when foreach loops to its last round, so that I can skip printing the last ":"? Thanks!
Sep 26 '08 #1
5 9175
numberwhun
3,509 Expert Mod 2GB
First, please use code tags, they are required.

Second, can you provide a sample of the data so we can see what you are talking about?

Regards,

Jeff
Sep 26 '08 #2
KevinADC
4,059 Expert 2GB
already answered on tek-tips

Expand|Select|Wrap|Line Numbers
  1. print join(':', map{$hash{$_}} keys %hash); 
Sep 27 '08 #3
for example assume i have an array i.e,
Expand|Select|Wrap|Line Numbers
  1. my @a = (1,2,3,4,5,6);
  2.  
  3. for(my $i = 0;$i<$#a;$i++) {
  4.          print $a[$i];
  5. }
  6.  
Above for loop we r excluding last element to print;
Sep 29 '08 #4
Ganon11
3,652 Expert 2GB
That's because you have i<$#a - use i<=$#a instead.

Better yet, use a foreach loop:

Expand|Select|Wrap|Line Numbers
  1. for my $element (@a) {
  2.    print $element;
  3. }
Sep 29 '08 #5
KevinADC
4,059 Expert 2GB
for example assume i have an array i.e,
Expand|Select|Wrap|Line Numbers
  1. my @a = (1,2,3,4,5,6);
  2.  
  3. for(my $i = 0;$i<$#a;$i++) {
  4.          print $a[$i];
  5. }
  6.  
Above for loop we r excluding last element to print;

Next time post a new thread.
Sep 29 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Mike P | last post by:
In a foreach loop, how do you break out of the iteration but not the loop? Break and continue both seem to exit the loop, I need something that for example on an if statement will cease execution...
13
by: TrintCSD | last post by:
How can I reset the collections within a foreach to be read as a change from within the foreach loop then restart the foreach after collections has been changed? foreach(string invoice in...
7
by: Robert Bravery | last post by:
HI all, If I have a foreach loop, is there a way to tell what number of itteration I am on during each loop Thanks Robert
3
by: cody | last post by:
Currently it is only legal to use types which has a method named GetEnumerator to be used in a foreach loop. This makes it impossible to use the same Enumerator after and before a foreach loop,...
23
by: Florian Lindner | last post by:
Hello, can I determine somehow if the iteration on a list of values is the last iteration? Example: for i in : if last_iteration: print i*i else:
10
by: fig000 | last post by:
HI, I'm new to generics. I've written a simple class to which I'm passing a generic list. I'm able to pass the list and even pass the type of the list so I can use it to traverse it. It's a...
1
by: Perl Beginner | last post by:
I hope i can articulate this question properly. i have been trying to figure this out for over a week. I am comparing the contents of two files, but the comparison is done inside of a foreach...
23
by: tshad | last post by:
Is there a way to know if you are looking at the last record record of foreach loop other then setting up a loop counter that you manually increment? foreach (Racecar racecar in...
15
by: fjm | last post by:
Hello everyone and happy Sunday. :) I have a problem that I *think* I may know the solution to but have no idea how to write the code for it. I am working on a templating system wher I have...
1
by: greyseal96 | last post by:
Hi, I am a pretty new programmer, so I apologize in andvance if this is a dumb question... In a book that I'm reading to learn C#, it says that when using a foreach() loop, a read-only copy of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.