Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Generate list from a to arbitrary letter

by kennethk (Monsignor)
on Nov 20, 2013 at 23:02 UTC ( #1063624=note: print w/ replies, xml ) Need Help??


in reply to [Solved] Generate list from a to arbitrary letter

Auto increment and Auto decrement:

The auto-increment operator has a little extra builtin magic to it. If you increment a variable that is numeric, or that has ever been used in a numeric context, you get a normal increment. If, however, the variable has been used in only string contexts since it was set, and has a value that is not the empty string and matches the pattern /^[a-zA-Z]*[0-9]*\z/, the increment is done as a string, preserving each character within its range, with carry:
print ++($foo = "99");    # prints "100"
print ++($foo = "a0");    # prints "a1"
print ++($foo = "Az");    # prints "Ba"
print ++($foo = "zz");    # prints "aaa"
So, you could generate your list with:
my $max = 703; my @letters; my $current = 'a'; for (1 .. $max) { push @letters, $current++; }
or some close variant.

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.


Comment on Re: Generate list from a to arbitrary letter
Select or Download Code
Re^2: Generate list from a to arbitrary letter
by boftx (Chaplain) on Nov 20, 2013 at 23:17 UTC

    I think this is a very easy-to-use approach. The only thing I would add would be a comment line reminding whoever has to maintain it down the road to read the docs for how the auto-increment operator works with string values if they are confused by what they see.

    It helps to remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.
Re^2: Generate list from a to arbitrary letter
by AnomalousMonk (Monsignor) on Nov 20, 2013 at 23:35 UTC
    ... a value that is not the empty string and matches the pattern  /^[a-zA-Z]*[0-9]*\z/ ...

    This part of the documentation is confusing to me because the empty string matches the pattern:

    >perl -wMstrict -le "$_ = ''; print 'match' if /^[a-zA-Z]*[0-9]*\z/; " match

    I'm sure I've seen a discussion of this point, but I cannot lay my hands on it at the moment. Anyone...?

      Well, there would be no point making two test if the result of one was the subset of the other (for exemple "a value that is positive and greater than 10"). In Perl, this condition would actually be: $matches if $_ ne '' and /^[a-zA-Z]*[0-9]*\z/ this was probably to have something easier to understand than one of those two patterns:

      /^(?: [a-zA-Z]+ [0-9]* | [a-zA-Z]* [0-9]+ ) \z/x; /^(?=.)[a-zA-Z]*[0-9]*\z/;
      The first one being a bit long, and the second not so beginner friendly.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1063624]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2014-07-11 00:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (217 votes), past polls