Detecting last match from within loop

N

Neil Shadrach

$ perl -wle 'foreach (q(/level1/level2/level3)=~m|/([^/]*)|g) { print }'
level1
level2
level3

What is the simplest way of determining from within the above loop whether it is the last iteration?
I could use an intermediate array ( below ) and compare a loop counter with $#a but I'm sure
some kind soul can point me at a neater way that is so obvious that I can't think of it :)

$ perl -wle '@a=q(/level1/level2/level3)=~m|/([^/]*)|g;foreach (@a) { print }'
level1
level2
level3
$
 
G

Gregory Toomey

Neil said:
$ perl -wle 'foreach (q(/level1/level2/level3)=~m|/([^/]*)|g) { print }'
level1
level2
level3

What is the simplest way of determining from within the above loop whether
it is the last iteration? I could use an intermediate array ( below ) and
compare a loop counter with $#a but I'm sure some kind soul can point me
at a neater way that is so obvious that I can't think of it :)

$ perl -wle '@a=q(/level1/level2/level3)=~m|/([^/]*)|g;foreach (@a) {
print }' level1
level2
level3
$

See this thread
http://tinyurl.com/3hmom

gtoomey
 
B

Ben Morrow

Neil Shadrach said:
$ perl -wle 'foreach (q(/level1/level2/level3)=~m|/([^/]*)|g) { print }'
level1
level2
level3

What is the simplest way of determining from within the above loop whether it is the last iteration?
I could use an intermediate array ( below ) and compare a loop counter with $#a but I'm sure
some kind soul can point me at a neater way that is so obvious that I can't think of it :)

$ perl -wle '@a=q(/level1/level2/level3)=~m|/([^/]*)|g;foreach (@a) { print }'
level1
level2
level3
$

If this is (as it appears to be) to split pathnames, then you have an
xy problem. Use File::Spec.

Ben
 
N

Neil Shadrach

Ben Morrow:
If this is (as it appears to be) to split pathnames, then you have an
xy problem. Use File::Spec.

The example was simplified to concentrate on the question.
What is actually being split is a string containing a series of regular expressions specifying a set of files of interest.
File::Spec doesn't appear to be useful for this.
Splitting into an array is fine as would be counting the separators - I just got thinking about it so I thought I'd ask.
Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top