String generation

P

petrucci

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
 
J

John W. Krahn

petrucci said:
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
 
T

Tassilo v. Parseval

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
 
T

Tassilo v. Parseval

Also sprach petrucci:
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
 
W

William James

petrucci said:
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}
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top