Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Re: creating all possible random 'words' from 4 letters

by blokhead (Monsignor)
on Nov 07, 2003 at 18:56 UTC ( [id://305408]=note: print w/replies, xml ) Need Help??


in reply to Re: creating all possible random 'words' from 4 letters
in thread creating all possible random 'words' from 4 letters

Using glob is pretty inventive. I like it, except that you generate a really big list*. As long as you're using all 26 letters, the increment operator will give you the list iteratively:
my $str = 'aaaa'; print $str++, $/ while $str le 'dddd';
No need to make an array with 26^4 elements!

OK, using increment is fun, so let's be goofy and do it with just [a-d]:

my $str = 'aaaa'; while ($str le 'dddd') { print $str++, $/; 1 while $str =~ s/(.)e/chr(1 + ord $1) . 'a'/e; }

* Update: BrowserUK++ for setting me straight with glob. You learn something new every day here!

blokhead

Replies are listed 'Best First'.
Re: Re: Re: creating all possible random 'words' from 4 letters
by BrowserUk (Patriarch) on Nov 07, 2003 at 19:05 UTC

    If you don't want them all in one list, just call glob in a scalar context and it will act as an iterator for you.

    perl -le" print while glob '{A,B,C,D}'x4 "

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    Hooray!
    Wanted!

      If you don't want them all in one list, just call glob in a scalar context and it will act as an iterator for you.

      ...but will still need to build the entire (easily huge) list into RAM, just not inside of Perl (and so will still fail except for near-trivial cases).

                      - tye

        Oh. Is that right? That wasn't how I read the POD for glob -- how disappointing.

        Thanks for setting me straight.


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail
        Hooray!
        Wanted!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2024-04-18 14:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found