Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Generate list from a to arbitrary letter

by kennethk (Abbot)
on Nov 20, 2013 at 23:02 UTC ( [id://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.

Replies are listed 'Best First'.
Re^2: Generate list from a to arbitrary letter
by AnomalousMonk (Archbishop) 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.

Re^2: Generate list from a to arbitrary letter
by boftx (Deacon) 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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-04-24 09:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found