String generation

Discussion in 'Perl Misc' started by petrucci, Sep 21, 2005.

  1. petrucci

    petrucci Guest

    Hi everyone..

    i want generate all the string starting a root

    For ex.:

    root = c

    result =
    ca
    cb
    cc
    cd
    ..
    ..
    cz
    caa
    cab
    ..
    ..
    caz
    cba
    ..
    ..
    cbz
    ..
    ..
    czz

    and go on even a predefined length
    Thank u
    petrucci, Sep 21, 2005
    #1
    1. Advertising

  2. petrucci wrote:
    >
    > i want generate all the string starting a root
    >
    > For ex.:
    >
    > root = c
    >
    > result =
    > ca
    > cb
    > cc
    > cd
    > .
    > .
    > cz
    > caa
    > cab
    > .
    > .
    > caz
    > cba
    > .
    > .
    > cbz
    > .
    > .
    > czz


    my $root = 'c';

    print "$root$_\n" for 'a' .. 'zz';



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Sep 21, 2005
    #2
    1. Advertising

  3. Also sprach petrucci:

    > i want generate all the string starting a root
    >
    > For ex.:
    >
    > root = c
    >
    > result =
    > ca
    > cb
    > cc
    > cd
    > .
    > .
    > cz
    > caa
    > cab
    > .
    > .
    > caz
    > cba
    > .
    > .
    > cbz
    > .
    > .
    > czz
    >
    > and go on even a predefined length


    This is quite easy with Perl's string-increment:

    use constant LENGTH => 3;

    my ($root, $tail) = qw/c a/;

    print $root . $tail++, "\n" while length("$root$tail") <= LENGTH;

    Tassilo
    --
    use bigint;
    $n=71423350343770280161397026330337371139054411854220053437565440;
    $m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);
    Tassilo v. Parseval, Sep 21, 2005
    #3
  4. petrucci

    petrucci Guest

    great!! and if i want to include simbols?
    petrucci, Sep 21, 2005
    #4
  5. Also sprach petrucci:
    > great!! and if i want to include simbols?


    Then you have to work harder as Perl's string increment doesn't handle
    symbols.

    Other than that, please use a proper quoting style when composing
    follow-up messages. You're supposed to quote context and to put your
    material below those quotes. For this and other essentials please read

    <http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html>

    first.

    Tassilo
    --
    use bigint;
    $n=71423350343770280161397026330337371139054411854220053437565440;
    $m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);
    Tassilo v. Parseval, Sep 21, 2005
    #5
  6. petrucci

    petrucci Guest

    > my ($root, $tail) = qw/c a/;

    what does it mean?
    petrucci, Sep 21, 2005
    #6
  7. Also sprach petrucci:

    >> my ($root, $tail) = qw/c a/;

    >
    > what does it mean?


    qw// is a list constructor. Items of such a list are
    whitespace-delimited and so the above is equivalent to

    my ($root, $tail) = ('c', 'a');

    qw is handy for lists where items never contain whitespace because you
    don't have to use tedious quotes around the items in case they are
    strings. See the entry on "qw/STRING/" under "Regexp Quote-Like
    Operators" in `perldoc perlop`.

    There are similar constructs for strings (q// and qq//), regexes (qr//)
    and backticks (qx//).

    Tassilo
    --
    use bigint;
    $n=71423350343770280161397026330337371139054411854220053437565440;
    $m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);
    Tassilo v. Parseval, Sep 21, 2005
    #7
  8. petrucci wrote:
    > Hi everyone..
    >
    > i want generate all the string starting a root
    >
    > For ex.:
    >
    > root = c
    >
    > result =
    > ca
    > cb
    > cc
    > cd
    > .
    > .
    > cz
    > caa
    > cab
    > .
    > .
    > caz
    > cba
    > .
    > .
    > cbz
    > .
    > .
    > czz
    >
    > and go on even a predefined length
    > Thank u


    You may prefer to use Ruby:

    root = "c"
    puts ("a".."zz").map{|s|root+s}
    William James, Sep 21, 2005
    #8
  9. petrucci

    Matija Papec Guest

    On 21 Sep 2005 01:35:50 -0700, "William James" <>
    wrote:

    >> and go on even a predefined length
    >> Thank u

    >
    >You may prefer to use Ruby:
    >
    >root = "c"
    >puts ("a".."zz").map{|s|root+s}


    Can you also offer perl equivalent so you wouldn't be completely
    offtopic?
    Matija Papec, Sep 21, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mladen Adamovic
    Replies:
    0
    Views:
    723
    Mladen Adamovic
    Dec 4, 2003
  2. Ken Human

    string generation

    Ken Human, May 22, 2005, in forum: C Programming
    Replies:
    27
    Views:
    775
    Keith Thompson
    May 27, 2005
  3. James Arnold

    String Generation using Mask Parsing

    James Arnold, Sep 21, 2008, in forum: C Programming
    Replies:
    3
    Views:
    761
    Ben Bacarisse
    Sep 21, 2008
  4. John W. Long

    HTML Generation (Next Generation CGI)

    John W. Long, Nov 22, 2003, in forum: Ruby
    Replies:
    4
    Views:
    317
    John W. Long
    Nov 24, 2003
  5. Derek Fountain

    Fast random string generation

    Derek Fountain, Oct 24, 2004, in forum: Perl Misc
    Replies:
    16
    Views:
    274
    Michele Dondi
    Oct 28, 2004
Loading...

Share This Page