Ranging to 'z'

J

John M. Gamble

I was reviewing my code, and came across a bit that made me say, "wait,
that can't work." Followed immediately by "why does that work?"

Here is the code reduced to it's essence:
my $inputs = 8;
my @alphabase = ('a'..chr($inputs));

The *intent* was to create a simple translation array (more complex
arrays are possible in this application, which is why i was going
through the bother of an array with this one). Now, according to my
naive reading of this code, this shouldn't produce anything useful at all.
But if you add the line
print @alphabase, "\n";
you get "abcdefghijklmnopqrstuvwxyz", which accidentally saved me.

Thing is, i don't think i deserved to be saved. Not because i am not
worthy (heh), but because this could be an error waiting to happen.
What if i wanted the length of the translation array later on, and i
used C<scalar @alphabase> instead of $inputs? Complications ensue.

The "Give 'em up to Z" feature can be seen in the even simpler one-
liner:
perl -le "print 'h'..'a'"

which prints out h through z. It's too consistent not be deliberate,
but what on Earth was it supposed to solve that someone thought it
was DWIM? Should this feature persist?

Thanks,
 
M

Malcolm Dew-Jones

John M. Gamble ([email protected]) wrote:
: I was reviewing my code, and came across a bit that made me say, "wait,
: that can't work." Followed immediately by "why does that work?"

: Here is the code reduced to it's essence:
: my $inputs = 8;
: my @alphabase = ('a'..chr($inputs));

: The *intent* was to create a simple translation array (more complex
: arrays are possible in this application, which is why i was going
: through the bother of an array with this one). Now, according to my
: naive reading of this code, this shouldn't produce anything useful at all.
: But if you add the line
: print @alphabase, "\n";
: you get "abcdefghijklmnopqrstuvwxyz", which accidentally saved me.

: Thing is, i don't think i deserved to be saved. Not because i am not
: worthy (heh), but because this could be an error waiting to happen.
: What if i wanted the length of the translation array later on, and i
: used C<scalar @alphabase> instead of $inputs? Complications ensue.

: The "Give 'em up to Z" feature can be seen in the even simpler one-
: liner:
: perl -le "print 'h'..'a'"

: which prints out h through z. It's too consistent not be deliberate,
: but what on Earth was it supposed to solve that someone thought it
: was DWIM? Should this feature persist?

perldoc perlop

The range operator (in list context) makes use of the magical
auto-increment algorithm if the operands are strings.
...
...If the final value specified is not
in the sequence that the magical increment would produce, the
sequence goes until the next value would be longer than the final
value specified.

I.e. When using strings, I guess this means the "range" can be magically
based on the length of the string, (magically here meaning "not all the
time", and coincidently also not what you anticipated some of the time).

In this case I guess the auto-increment ran out of values before it found
one that was longer, and so the last available auto-increment value (z)
became the last value.
 
J

John M. Gamble

John M. Gamble ([email protected]) wrote:
: I was reviewing my code, and came across a bit that made me say, "wait,
: that can't work." Followed immediately by "why does that work?"

: Here is the code reduced to it's essence:
: my $inputs = 8;
: my @alphabase = ('a'..chr($inputs));

: The *intent* was to create a simple translation array (more complex
: arrays are possible in this application, which is why i was going
: through the bother of an array with this one). Now, according to my
: naive reading of this code, this shouldn't produce anything useful at all.
: But if you add the line
: print @alphabase, "\n";
: you get "abcdefghijklmnopqrstuvwxyz", which accidentally saved me.

: Thing is, i don't think i deserved to be saved. Not because i am not
: worthy (heh), but because this could be an error waiting to happen.
: What if i wanted the length of the translation array later on, and i
: used C<scalar @alphabase> instead of $inputs? Complications ensue.

: The "Give 'em up to Z" feature can be seen in the even simpler one-
: liner:
: perl -le "print 'h'..'a'"

: which prints out h through z. It's too consistent not be deliberate,
: but what on Earth was it supposed to solve that someone thought it
: was DWIM? Should this feature persist?

perldoc perlop

The range operator (in list context) makes use of the magical
auto-increment algorithm if the operands are strings.
...
...If the final value specified is not
in the sequence that the magical increment would produce, the
sequence goes until the next value would be longer than the final
value specified.

"... until the next value would be longer than the final
value specified."

It took me a while to parse that sentence. Fortunately, your next
paragraph helped a lot.
I.e. When using strings, I guess this means the "range" can be magically
based on the length of the string, (magically here meaning "not all the
time", and coincidently also not what you anticipated some of the time).

Gotcha. "Longer," not "Larger." String length, not value size.
In this case I guess the auto-increment ran out of values before it found
one that was longer, and so the last available auto-increment value (z)
became the last value.

Yeah, that's still a little strange to me. I think i would have
preferred an outright error message.

Thank you very much.
 

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
474,143
Messages
2,570,821
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top