[Tutor] Perl equivalent of $#var

Blake Winton bwinton at latte.ca
Wed May 18 19:47:44 CEST 2005


> My current dilemma is that I've got a program that takes one argument
> and needs to be run multiple times with this argument being validated
> based on the previous one.  So proper usage might be
> 	myprog red
> 	myprog blue
> 	myprog green
> where it would be wrong to do
> 	myprog red
> 	myprog green	
> I have a list which gives the proper order
> 	colors = ['red', 'blue', 'green']

I've got to say, that's an odd setup you've got there.
If it really needs to run in the proper order, why not
just take no arguments, and put the whole program in a
	for color in colors:
loop?  Or take no arguments, store the last run in a file,
and 

> 	if now == len(colors) - 1
> which is distinctly ugly and I would prefer
> 	if now == $#colors

Perhaps it would look nicer to you if you had:
	colors = ['red', 'blue', 'green']
	LAST_COLOR = len(colors) - 1
[...]
 	if now == LAST_COLOR:
		# Do something.

Later,
Blake.



More information about the Tutor mailing list