CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
List generation

 



cold_one
Novice

Feb 18, 2006, 8:06 PM

Post #1 of 21 (2391 views)
List generation Can't Post

Hi guys, used to be on here by the nick of always in chaos a few years back. Anyways, I yet again have another noobish question .

Is it possible ( and how ? ) to do the following via perl :

input a string (ex. abcdefghijklmnopqrsuvwxyz_1234567890) characters to use
input a interger (ex 1-999) # of character spaces
input a word (ex. blue) word that goes after the previous 2

so the output would be :
abcdefghijklmnopqrsuvwxyz_1234567890 (string)
2 characters( example )
blue (word)

aablue
abblue
acblue
adblue
etc, outputed as a text file ?


(This post was edited by cold_one on Feb 18, 2006, 8:35 PM)


davorg
Thaumaturge / Moderator

Feb 20, 2006, 8:26 AM

Post #2 of 21 (2379 views)
Re: [cold_one] List generation [In reply to] Can't Post

I'm certain that Perl will be able to do what you are asking. But I'm afraid I'm not entirely sure exactly what you are asking.

Perhaps you could try explaining again.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


cold_one
Novice

Feb 20, 2006, 1:33 PM

Post #3 of 21 (2375 views)
Re: [davorg] List generation [In reply to] Can't Post

i'm more or less asking how to do it . maybe this is a bit more descriptive ( what the program might look like while running ) List Generation v 1.x Please input total number of characters that you want before keyword : ex : 4 Please input the string of characters that you want to use before keyword : ex: ab12 Please input keyword : ex: blue **** Generating List **** outputed to list.txt aaaablue aaabblue aaa1blue aaa2blue aabablue aa1ablue etc so it list all possible combinations using those 4 letters then adds the key word to the end.


KevinR
Veteran


Feb 20, 2006, 1:46 PM

Post #4 of 21 (2374 views)
Re: [cold_one] List generation [In reply to] Can't Post

is this some type of school/class work assingment?
-------------------------------------------------


davorg
Thaumaturge / Moderator

Feb 20, 2006, 2:12 PM

Post #5 of 21 (2371 views)
Re: [cold_one] List generation [In reply to] Can't Post

I'm afraid that doesn't really help. What I need is a description of what exactly the program does.

I know you get three inputs. But you haven't explained what transformation the inputs need to go through in order to create the outputs. And I don't mean just give me another example. I mean explain the processing.

Once you understand the processing, then converting that into a program is usually pretty simple.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


cold_one
Novice

Feb 20, 2006, 2:12 PM

Post #6 of 21 (2371 views)
Re: [KevinR] List generation [In reply to] Can't Post

nope


KevinR
Veteran


Feb 20, 2006, 4:51 PM

Post #7 of 21 (2369 views)
Re: [cold_one] List generation [In reply to] Can't Post

install the List::Permutor module or download the source and use/require it from your cgi-bin:


Code
use List::Permutor; 

my $string = 'ab12';
my $integer = 4;
my $word = 'blue';
my @characters = split(//,$string);
if(@characters > $integer) {
print "Error: too many characters is string\n";
exit(0);
}
my $perm = new List::Permutor @characters;
while (my @set = $perm->next) {
print join('',@set)."$word\n";
}


The suggestion for the module was in the perl faqs4 (arrays)

http://perldoc.perl.org/perlfaq4.html#How-do-I-permute-N-elements-of-a-list%3f

the documentation in the above link was a little different than the code I have above but this is what worked after I installed the list::Permutor module and read the documentation of the module.
-------------------------------------------------


(This post was edited by KevinR on Feb 20, 2006, 6:28 PM)


cold_one
Novice

Feb 21, 2006, 4:23 PM

Post #8 of 21 (2338 views)
Re: [KevinR] List generation [In reply to] Can't Post

Thanks Kevin, Can you give me a link to where you dl that module ?
and how do I install it to use it the same way you did ?

do I put it in the perl\bin directory?


KevinR
Veteran


Feb 21, 2006, 10:34 PM

Post #9 of 21 (2333 views)
Re: [cold_one] List generation [In reply to] Can't Post

I think I downloaded it from activestate since I used ppm to install the module on my local windows/apache server. But you can get the module from cpan too:

http://search.cpan.org/~phoenix/List-Permutor-0.022/Permutor.pm

if you can't install the module or don't know how to install modules, click the "source" link on the above page (at the top of the page). Copy and paste the source code into a text document and name it "Permutor.pm" and place it in your cgi-bin then you can call it like so:

use Permutor;

and in the script call it using Permutor instead of List::Permutor

my $perm = new Permutor @characters;

that should work.
-------------------------------------------------


cold_one
Novice

Feb 22, 2006, 3:50 AM

Post #10 of 21 (2329 views)
Re: [KevinR] List generation [In reply to] Can't Post


Code
use Permutor; 
my $string = 'ab12';
my $integer = 4; my $word = 'blue';
my @characters = split(//,$string);
if(@characters > $integer) { print "Error: too many characters is string\n"; exit(0); }
my $perm =new Permutor @characters;
while (my @set = $perm->next) { print join ('',@set)."$word\n"; }


That's the code that I'm using, file is @ htdocs\test.pl Permutor.pm is in cgi-bin\Permutor.pm This is what I get :

Quote
C:\Perl\bin>perl c:\apache\htdocs\test.pl
Can't locate object method "new" via package "Permutor" (perhaps you forgot to l
oad "Permutor"?) at c:\apache\htdocs\test.pl line 6.

What am I doing wrong now ;/


KevinR
Veteran


Feb 22, 2006, 9:33 AM

Post #11 of 21 (2316 views)
Re: [cold_one] List generation [In reply to] Can't Post

sorry, hopefully one last thing, in the Permutor.pm module, the first line is:

package List::Permutor;

change it to:

package Permutor;
-------------------------------------------------


cold_one
Novice

Feb 22, 2006, 4:48 PM

Post #12 of 21 (2312 views)
Re: [KevinR] List generation [In reply to] Can't Post

Still get same error, can you check your messages? Ty.


KevinR
Veteran


Feb 22, 2006, 5:06 PM

Post #13 of 21 (2311 views)
Re: [cold_one] List generation [In reply to] Can't Post

Sorry, no IM.

The code works fine for me:


Code
use Permutor;  
my $string = 'ab12';
my $integer = 4; my $word = 'blue';
my @characters = split(//,$string);
if(@characters > $integer) { print "Error: too many characters is string\n"; exit(0); }
my $perm =new Permutor @characters;
while (my @set = $perm->next) { print join ('',@set)."$word\n"; }


and Permutor.pm in the cgi-bin with the above script:


Code
package Permutor; 

use strict;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);

require Exporter;

@ISA = qw(Exporter);
@EXPORT = qw();
$VERSION = '0.022';

sub new {
my $class = shift;
my $items = [ @_ ];
bless [ $items, [ 0..$#$items ] ], $class;
}

sub reset {
my $self = shift;
my $items = $self->[0];
$self->[1] = [ 0..$#$items ];
1; # No useful return value
}

sub peek {
my $self = shift;
my $items = $self->[0];
my $rv = $self->[1];
@$items[ @$rv ];
}

sub next {
my $self = shift;
my $items = $self->[0];
my $rv = $self->[1]; # return value array
return unless @$rv;
my @next = @$rv;
# The last N items in @next (for 1 <= N <= @next) are each
# smaller than the one before. Move those into @tail.
my @tail = pop @next;
while (@next and $next[-1] > $tail[-1]) {
push @tail, pop @next;
}
# Then there's one more. Right?
if (defined(my $extra = pop @next)) {
# The extra one exchanges with the next larger one in @tail
my($place) = grep $extra < $tail[$_], 0..$#tail;
($extra, $tail[$place]) = ($tail[$place], $extra);
# And the next order is what you get by assembling the three
$self->[1] = [ @next, $extra, @tail ];
} else {
# Guess that's all....
$self->[1] = [];
}
return @$items[ @$rv ];
}

1;


prints:


Code
ab12blue 
ab21blue
a1b2blue
a12bblue
a2b1blue
a21bblue
ba12blue
ba21blue
b1a2blue
b12ablue
b2a1blue
b21ablue
1ab2blue
1a2bblue
1ba2blue
1b2ablue
12abblue
12bablue
2ab1blue
2a1bblue
2ba1blue
2b1ablue
21abblue
21bablue

-------------------------------------------------


cold_one
Novice

Feb 22, 2006, 5:20 PM

Post #14 of 21 (2308 views)
Re: [KevinR] List generation [In reply to] Can't Post

am I not running it right? do I need to set my path to include all perl directories + apache directories when running?


KevinR
Veteran


Feb 23, 2006, 1:08 AM

Post #15 of 21 (2304 views)
Re: [cold_one] List generation [In reply to] Can't Post

all you need to do is make sure the script and the Permutor.pm module are in the same directory when you run the script. Are you running it command line or as a cgi script from a browser? It should work either way, it does for me.
-------------------------------------------------


cold_one
Novice

Feb 23, 2006, 1:16 AM

Post #16 of 21 (2302 views)
Re: [KevinR] List generation [In reply to] Can't Post

it's working now, i feel foolish, lol I had the pm file where it belonged and stuff, but cause I just moved it there, it didnt work I ended up using ppm to install it correctly, now I just have to get it so it prints those outputs to a text file,:)


cold_one
Novice

Feb 23, 2006, 2:29 AM

Post #17 of 21 (2299 views)
Re: [cold_one] List generation [In reply to] Can't Post

hmm now why doesnt this work ;/

Code
 use List::Permutor; 
my $string = 'ab12';
my $integer = 4;
my $word = 'blue';
my @characters = split(//,$string);
if(@characters > $integer) {
print "Error: too many characters is string\n";
exit(0);
}
my $perm = new List::Permutor @characters;
while (my @set = $perm->next) {
open (output,"output.txt");
print output join('',@set)."$word\n"; close (output); }



KevinR
Veteran


Feb 23, 2006, 9:30 AM

Post #18 of 21 (2295 views)
Re: [cold_one] List generation [In reply to] Can't Post


Code
use List::Permutor;  
my $string = 'ab12';
my $integer = 4;
my $word = 'blue';
my @characters = split(//,$string);
if(@characters > $integer) {
print "Error: too many characters is string\n";
exit(0);
}
my $perm = new List::Permutor @characters;
open (OUT, '>output.txt') or die "$!";
while (my @set = $perm->next) {
print OUT join('',@set)."$word\n";
}
close(OUT);

-------------------------------------------------


cold_one
Novice

Feb 24, 2006, 5:37 AM

Post #19 of 21 (2282 views)
Re: [KevinR] List generation [In reply to] Can't Post

Hmm, this only works if the string is = to the interger, is there a way to make it so the string is > then the integer? so say i have 6 letters in the string and want the list for every 4 letter possibility. right now it only will work if I ask for the 6 letter possiblity.


KevinR
Veteran


Feb 26, 2006, 11:33 AM

Post #20 of 21 (2272 views)
Re: [cold_one] List generation [In reply to] Can't Post

I am sure it's possible, but I really don't understand what you are asking or what you are doing. I based the code I posted on your original question to get you started, I assumed you would be able to take over from there and adjust the code to your needs.
-------------------------------------------------


davorg
Thaumaturge / Moderator

Feb 28, 2006, 3:23 AM

Post #21 of 21 (2265 views)
Re: [cold_one] List generation [In reply to] Can't Post

Use Data::PowerSet and only use the sets which contain 4 elements.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives