Re: [Tutor] Perl equivalent of $#var
> 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. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Perl equivalent of $#var
On 5/18/05, Smith, Jeff <[EMAIL PROTECTED]> wrote: > Is there a more Pythonic way to get the Perl equivalent of > $#var > other than > len(var) - 1 By Pythonic, if you mean OO, you can do this: ['foo', 2, 'baz'].__len__() -- Premshree Pillai http://www.livejournal.com/users/premshree/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Perl equivalent of $#var
The most common usage is to get the last member of an array as with myarray[$#myarray] and I realize in Python, this can be done with myarray[-1] 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'] There are places where I need to know specifically if I am on the first or last sequence of runs so early on I get my current index: now = colors.index(sys.argv[1]) and then later I can compare if now == 0 or if now == len(colors) - 1 which is distinctly ugly and I would prefer if now == $#colors Keep in mind this is not an exact statement of the problem but I believe it captures the full context. Jeff -Original Message- From: Danny Yoo [mailto:[EMAIL PROTECTED] Sent: Tuesday, May 17, 2005 6:00 PM To: Smith, Jeff Cc: tutor@python.org Subject: Re: [Tutor] Perl equivalent of $#var On Tue, 17 May 2005, Smith, Jeff wrote: > Is there a more Pythonic way to get the Perl equivalent of > $#var > other than > len(var) - 1 Hi Jeff, Just out of curiosity, where do you use Perl's $#var? Can you show us the context of its use? If we see context, it might help us find a Python idiom that fits the usage. Best of wishes! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Perl equivalent of $#var
On Tue, 17 May 2005, Smith, Jeff wrote: > Is there a more Pythonic way to get the Perl equivalent of > $#var > other than > len(var) - 1 Hi Jeff, Just out of curiosity, where do you use Perl's $#var? Can you show us the context of its use? If we see context, it might help us find a Python idiom that fits the usage. Best of wishes! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Perl equivalent of $#var
On May 17, 2005, at 22:00, Smith, Jeff wrote: > Is there a more Pythonic way to get the Perl equivalent of > $#var > other than > len(var) - 1 AFAIK, len(var) - 1 is the only way. Note, however, that the last element of a list (or of any ordered sequence) can be obtained with the shortcut var[-1]. -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Perl equivalent of $#var
Is there a more Pythonic way to get the Perl equivalent of $#var other than len(var) - 1 Thanks, Jeff ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor