Re: AW: compact my wordlist generator
A bit of CS philosophy if you may... On Monday 26 Oct 2009 18:41:23 Jim Gibson wrote: > On 10/26/09 Mon Oct 26, 2009 8:45 AM, "Michael Alipio" > > scribbled: > > Thanks for the advice. Forgive me if I sounded like someone who's > > frustrated, couldn't do his homework asking somebody else for help. > > > > When I was learning C programming, I read that learning those difficult > > algorithms such as bubble sort, quick sort, binary search, is something > > that only programming students have to deal with. > > Those are not difficult algorithms. They are algorithms that any programmer > should know. If you are learning how to program, then you are a > "programming student". However, generating a complete set of permutations > efficiently for any size set IS a difficult algorithm. > > > I knew I needed a recursive function, I just didn't know how to start. > > What confused me more is that the code I found is a lot different from > > what I was initially thinking. He used the builtin function substr in the > > code in a way that I couldn't figure how it worked. > > You don't "need" a recursive function. Using a recursive function can > sometimes be an elegant method for solving a difficult problem. However, > alternate solutions not using recursive functions can always be found. > One thing I should note is that from my understanding of theoretical or pseudo-theoretical computer science, a function or routine that just uses its own stack to do tree-recursion, is still recursive, only it is not *procedurally-recursive*. Often, people use such techniques of doing a recursion without a procedure that calls itself directly or indirectly, when writing programs in hardware-design languages such as VHDL or Verilog (or so I was told - I have little serious experience with them myself), or in languages that do not support such recursion (such as COBOL or old versions of Fortran), but it is a useful in languages that do support such recursion too. In one of my C libraries ( http://fc-solve.berlios.de/ to be specific) I ended up porting a procedurally recursive Depth-First Search ( http://en.wikipedia.org/wiki/Depth-first_search ) algorithm to one that used my own dedicated stack of items, in order to save on CPU stack space, which is limited, especially the Windows' default. It turned out to be beneficial because that way I could do a constant time ( O(1) ) suspend and resume of the algorithm without resorting to setjmp/longjmp games (which I naturally would prefer to avoid). In some languages (most notably Scheme), proper tail recursion is implemented using an implicit goto. So the following Perl program when translated to Scheme: sub print_loop { my ($i) = @_; print "I is now $i\n"; if ($i == 0) { return; } else { return print_loop($i-1); } } Would be equivalent to: sub print_loop { my ($max_i) = @_; foreach my $i (reverse(0 .. $max_i)) { print "I is now $i\n"; } return; } You can achieve a similar effect in Perl explicitly using "goto &subname;" but I wouldn't recommend it. From what I understood, the Perl's recursion stack (and all the other stacks used by the Perl back-end) are implemented on the process heap so they cannot overflow the process stack, unless one recursively uses binary Perl-XS routines with Perl callbacks (e.g: those in List::Util or List::MoreUtils). Regards, Shlomi Fish > > See below: > > > > my $result = ''; > > > > perm(0,2); > > > > sub perm($$){ > > my ($cur,$max) = @_; > > > > if ($cur>=$max){ > > print "$result\n"; > > return; > > } > > > > for(@word){ > > substr($result,$cur,1)=$_; > > perm($cur+1,$max); > > } > > } > > > > What's the use of double ($$) after sub perm?? Does it have anything to > > do with process IDs (perldoc perlvar says so)? > > The ($$) is a function prototype indicating that the function accepts two > scalars. See 'perldoc perlsub' and search for 'Prototypes'. > > > This line is also confusing: > > substr($result,$cur,1)= $_; > > > > Substr returns a list, right? The first parameter is the expression, 2nd > > is the offset, and last is the length. > > The above line is too cryptic for me. What confused me more is that the > > return value of substr was assigned the $_. > > No, substr returns a scalar. It can also return an lvalue if its first > argument is an lvalue, as is being done in this case. This allows you to > assign to a subset of a string scalar the value appearing to the right of > the equal sign ($_), thereby replacing part of the string. You can perform > the same function by including the replacement part as a fourth argument to > substr. See 'perldoc -f substr' for details. > -- - Shlomi Fish http://www.shlomifish.org/ "The Human Hacking Field Guide" - http://shlom.in/hhf
Re: compact my wordlist generator
2009/10/26 Michael Alipio : > Then as suggested (see far below), a recursive function will do what I want.. > After some googling I found this: > > > permutate(0,2); > > sub permutate($$){ > > my ($cur,$max) = @_; > > if ($cur>=$max){ > print "$result\n"; > return; > } > > for(@word){ > substr($result,$cur,1)=$_; > perm($cur+1,$max); > } > } > > > Can anyone tell me how the code above works? Yes, not very well: Undefined subroutine &main::perm called at foo.pl line 14. ie you defined 'sub permutate' but you called 'perm()'. Please, please, post *working* code if you want us to help you. I know what's wrong and I can fix it myself, but you do yourself no favours by making those who would help you work harder. > My original program must deal with arbitrary length and generate all the > possible combinations (even repeating) of a particular set. What could take > me gazillions of for loops for that, somebody just came up with less than ten > lines. > I can just copy and paste the code I found above but I want to learn how it > works. How could someone write this code so easily but when I tried even > writing just a pseudocode for it, my head almost exploded. > > I want to be a good programmer, but at this rate, I don't think I can be one > if I will just keep copying and pasting someone else's code or downloading > modules from CPAN. > > Can anyone teach me how my own code (the one with gazillion for loops), can > be converted into a pseudocode then eventually into a working sub procedure? The code you found will teach you bad habits. Among other things: * it uses the package variable $result to communicate between recursive calls, rather than (in my version posted to the list a few days ago) in an extra argument. This is a Bad Thing. * it uses prototypes. Don't use prototypes; long story http://xrl.us/bffo9k ; short story: prototypes are not like C prototypes. They don't give you type safety. They don't force you to use a scalar where it expects a scalar argument - you can still provide an array or hash. And when you do, it will give you confusing, hard-to-track-down bugs. I would recommend you look instead at the code I posted to the list, and I'll be happy to explain it to you. As for how to "think in recursion", I find the best way to think is this way: * How would I do it for a trivial case? * Given I can do it for a case of size N-1, how would I do it for a case of size N? * How do I express this in perl? As an example, the factorial function, the three steps become: * How do I calculate 0!? Answer: it is 1 by definition. * Given that I can calculate (N-1)!, how do I calculate N! ? Answer: I can calculate it thus: N * (N-1)! * How do I express this in perl? sub fact { my ($n) = @_; die "Can't calculate factorial of a negative or fractional number" if $n<0; if ($n == 0) { # Base case return 1; } # Solve fact($n) given we can already do fact($n-1) return $n * fact ($n-1); } If you know your maths, this process is very similar to "proof by induction". Philip -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: printf with currency symbols
> Robert Citek wrote: >> Not sure if there is a better way. My guess is that there is probably >> some module to convert float to currency and then print it as a >> string. But a quick Google didn't turn up anything. > > Here' why (extracted from `perldoc perllocale`): > >Category LC_MONETARY: Formatting of monetary amounts > >The C standard defines the "LC_MONETARY" category, but no > function that is affected by its contents. (Those with experience of > standards committees will recognize that the working group decided to > punt on the issue.) Consequently, Perl takes no notice of it. If you > really want to use "LC_MONETARY", you can query its contents--see "The > localeconv function"--and use the information that it returns in your > application¹s own formatting of currency amounts. However, you may well > find that the information, voluminous and complex though it may be, > still does not quite meet your requirements: currency formatting is a > hard nut to crack. That's what I needed to know -- thanks Shawn (and Jim and Robert). - Bryan -- Bryan Harris Sr. Systems Engineer II Huntsville Operations Analysis & System Performance Missile Systems, Raytheon Company b...@raytheon.com 256.542.4632 -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: printf with currency symbols
Robert Citek wrote: > Not sure if there is a better way. My guess is that there is probably > some module to convert float to currency and then print it as a > string. But a quick Google didn't turn up anything. Here' why (extracted from `perldoc perllocale`): Category LC_MONETARY: Formatting of monetary amounts The C standard defines the "LC_MONETARY" category, but no function that is affected by its contents. (Those with experience of standards committees will recognize that the working group decided to punt on the issue.) Consequently, Perl takes no notice of it. If you really want to use "LC_MONETARY", you can query its contents--see "The localeconv function"--and use the information that it returns in your application’s own formatting of currency amounts. However, you may well find that the information, voluminous and complex though it may be, still does not quite meet your requirements: currency formatting is a hard nut to crack. -- Just my 0.0002 million dollars worth, Shawn Programming is as much about organization and communication as it is about coding. I like Perl; it's the only language where you can bless your thingy. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: printf with currency symbols
I see. You want the output to look something like this: $ perl -e 'for(my $total = 24.15; $total <3; $total *= 10) { printf("Total:%10s\n", "\$" . sprintf("%.2f",$total)) ;} ' Total:$24.15 Total: $241.50 Total: $2415.00 Total: $24150.00 Not sure if there is a better way. My guess is that there is probably some module to convert float to currency and then print it as a string. But a quick Google didn't turn up anything. Good luck and let us know how things go. Regards, - Robert On Mon, Oct 26, 2009 at 11:57 AM, Bryan R Harris wrote: > Is there a way to do this without getting all messy like this? > > printf "Total:%10s\n", "\$".sprintf(%.2f,$total); -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: printf with currency symbols
Is this what you are looking for: $ perl -e '$total = 24.15 ; printf "Total: \$%.2f\n", $total; ' Regards, - Robert On Mon, Oct 26, 2009 at 11:57 AM, Bryan R Harris wrote: > Is there a good way to do printf's with currency symbols? > > I've tried this: > > printf "Total: \$%10.2f\n", $total; > > But it puts the dollar sign way out front (ugly). I want it to look like: > > Total: $24.15 > > Is there a way to do this without getting all messy like this? > > printf "Total:%10s\n", "\$".sprintf(%.2f,$total); -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
AW: AW: compact my wordlist generator
Michael Alipio wrote: > I knew I needed a recursive function, I just didn't know how to start. That is usually the easy part - you start out by solving the problem for a base case, and then you embellish. With regard to your problem of generating all combinations of a set of elements, the base case would be to just enumerate those elements. For a longer result set with n elements (n being > 1), you'd then combine the result for n-1 elements with the one element result set. > What confused me more is that the code I found is a lot different from > what I was initially thinking. He used the builtin function substr in the > code in a way that I couldn't figure how it worked. If in doubt, it pays off to RTFM ;-) So you do "perldoc -f substr" and it'll tell you: "You can use the substr() function as an lvalue, in which case EXPR must itself be an lvalue." It helps to know that lvalue is short for "left-hand value" and it basically means that it behaves like a variable on the left hand side of the assignment operator - it can be assigned a value. The example from the man page helps to clarify this behavior: my $name = 'fred'; substr($name, 4) = 'dy'; # $name is now 'freddy' > What's the use of double ($$) after sub perm?? Does it have anything to > do with process IDs (perldoc perlvar says so)? Nope, these are prototypes for the function call (see perldoc perlsub, section Prototype). Basically these two $$ signs make the function request two arguments, which will be coerced to scalar values. This is of course only half the truth, but I'll leave the nasty details and consequences to more erudite folks on this list to explain ;-) > This line is also confusing: > substr($result,$cur,1)= $_; > > Substr returns a list, right? The first parameter is the expression, 2nd > is the offset, and last is the length. > The above line is too cryptic for me. What confused me more is that the > return value of substr was assigned the $_. Actually, substr() as an lvalue designates a part of the string $result that will be assigned to, but please see above. BTW, taking the recursive approach outlined above and using Iterators, my first draft for a combination generator looks like this: #!/usr/bin/perl -w use strict; # # Iterator that generates all combinations of length $n of a set of elements. # sub gen_combo_iterator { my( $n, @elements ) = @_; # base case: iterator for all one element combinations # This will just enumerate all elements of the set if( $n == 1 ){ # this is a so-called closure - we return an anonymous subroutine that # can still access the variables in its outer scope even though they # are no longer visible for the rest of the program. # subsequent calls to the anonymous function will manipulate a copy # of the variable @elements that was created when the function was # created by calling the outer subroutine. return sub { return shift @elements; }; } elsif( $n > 1 ){ # recursion: all combinations of n elements are the product # of all (n-1) element combinations x all 1 element combinations. # this call with fall through until it reaches $n = 1 my $sub_combo_iterator = gen_combo_iterator( $n -1 => @elements ); # the second iterator has just one element my $combo_iterator = gen_combo_iterator( 1 => @elements ); # we pick the first element my $element = $combo_iterator->(); return sub { if( my $sub_combo = $sub_combo_iterator->() ){ # if the iterator over all combinations of length n-1 still # returns a value, combine it with my current item. return $element . $sub_combo; } else { # try and pick a new element from the 1 element iterator if( $element = $combo_iterator->() ){ # if there's a new element, reset the n-1 element iterator $sub_combo_iterator = gen_combo_iterator( $n -1 => @elements ); # get a fresh element from the n-1 element iterator $sub_combo = $sub_combo_iterator->(); # combine with the current element and return like above return $element . $sub_combo; } else { # nothing more to return, we are finished. return undef; } } }; } else { die "Assertion: $n must be a positive inter greater or equal 1"; } } # # generate an iterator that will return all combinations of a given set # with $min up to $max elements. # sub gen_all_combinations_iterator { my( $min, $max, @elements ) = @_; if( $min < $max ){ my $iterator = gen_combo_iterator( $min => @elements ); return sub { if( my $result = $iterator->() ){ return $result; } else { $min++; if( $min <= $max ){ $iterator = gen_combo_iterator( $min => @elements ); return $iterator->();
Re: Dear friend!
On Mon, Oct 26, 2009 at 12:06 PM, Pat Rice wrote: > Dear friend,I am willing to give you a big surprise. > Friend, I'm willing to give you some good advice! use warnings; use strict; Read http://perldoc.perl.org/ Ian
Dear friend!
Dear friend,I am willing to give you a big surprise. I found a website: www.ollsu.com last week. They mainly sell phones , laptops, tvs ,digital cameras and motorbikes. I ordered a tv . now I have got the product after 5 days. Its quality is very good.By the way, they only sell new and original products and their products have international warranty. Now , they are promoting their products, so the prices are very competitives. If you need these products, you can have a look. Don not miss the good chance !Best wishes! -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: AW: compact my wordlist generator
On 10/26/09 Mon Oct 26, 2009 8:45 AM, "Michael Alipio" scribbled: > Thanks for the advice. Forgive me if I sounded like someone who's frustrated, > couldn't do his homework asking somebody else for help. > > When I was learning C programming, I read that learning those difficult > algorithms such as bubble sort, quick sort, binary search, is something that > only programming students have to deal with. Those are not difficult algorithms. They are algorithms that any programmer should know. If you are learning how to program, then you are a "programming student". However, generating a complete set of permutations efficiently for any size set IS a difficult algorithm. > I knew I needed a recursive function, I just didn't know how to start. > What confused me more is that the code I found is a lot different from what I > was initially thinking. He used the builtin function substr in the code in a > way that I couldn't figure how it worked. You don't "need" a recursive function. Using a recursive function can sometimes be an elegant method for solving a difficult problem. However, alternate solutions not using recursive functions can always be found. > > See below: > > my $result = ''; > > perm(0,2); > > sub perm($$){ > my ($cur,$max) = @_; > > if ($cur>=$max){ > print "$result\n"; > return; > } > > for(@word){ > substr($result,$cur,1)=$_; > perm($cur+1,$max); > } > } > > What's the use of double ($$) after sub perm?? Does it have anything to do > with process IDs (perldoc perlvar says so)? The ($$) is a function prototype indicating that the function accepts two scalars. See 'perldoc perlsub' and search for 'Prototypes'. > > This line is also confusing: > substr($result,$cur,1)= $_; > > Substr returns a list, right? The first parameter is the expression, 2nd is > the offset, and last is the length. > The above line is too cryptic for me. What confused me more is that the return > value of substr was assigned the $_. No, substr returns a scalar. It can also return an lvalue if its first argument is an lvalue, as is being done in this case. This allows you to assign to a subset of a string scalar the value appearing to the right of the equal sign ($_), thereby replacing part of the string. You can perform the same function by including the replacement part as a fourth argument to substr. See 'perldoc -f substr' for details. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: printf with currency symbols
On 10/26/09 Mon Oct 26, 2009 8:57 AM, "Bryan R Harris" scribbled: > > > Is there a good way to do printf's with currency symbols? > > I've tried this: > > printf "Total: \$%10.2f\n", $total; > > But it puts the dollar sign way out front (ugly). I want it to look like: > > Total:$24.15 You can add a minus sign to the format descriptor to left-justify the field: %-10.2f However, if you do this and print more than one line, the decimal points may not line up. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
printf with currency symbols
Is there a good way to do printf's with currency symbols? I've tried this: printf "Total: \$%10.2f\n", $total; But it puts the dollar sign way out front (ugly). I want it to look like: Total:$24.15 Is there a way to do this without getting all messy like this? printf "Total:%10s\n", "\$".sprintf(%.2f,$total); - Bryan -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: AW: compact my wordlist generator
Thanks for the advice. Forgive me if I sounded like someone who's frustrated, couldn't do his homework asking somebody else for help. When I was learning C programming, I read that learning those difficult algorithms such as bubble sort, quick sort, binary search, is something that only programming students have to deal with. I have also learned that the best way to learn is by looking at other peoples code. So far I managed to write a number of useful perl programs through this. I just got frustrated with this particular task I had to accomplish. I've done some readings before about those math topics you've mentioned. As I understood, permutation is producing all combinations without repetition. A part of my code uses the module Algorithm::Permute for this purpose and for the other part which produces all possible (repeating) combinations, I have to write this code. Somebody gave me an example pseudocode and I tried to convert my code into it but I just couldn't do it. I knew I needed a recursive function, I just didn't know how to start. What confused me more is that the code I found is a lot different from what I was initially thinking. He used the builtin function substr in the code in a way that I couldn't figure how it worked. See below: my $result = ''; perm(0,2); sub perm($$){ my ($cur,$max) = @_; if ($cur>=$max){ print "$result\n"; return; } for(@word){ substr($result,$cur,1)=$_; perm($cur+1,$max); } } What's the use of double ($$) after sub perm?? Does it have anything to do with process IDs (perldoc perlvar says so)? This line is also confusing: substr($result,$cur,1)= $_; Substr returns a list, right? The first parameter is the expression, 2nd is the offset, and last is the length. The above line is too cryptic for me. What confused me more is that the return value of substr was assigned the $_. If someone can help me decipher each line, i'll be very happy. --- On Mon, 10/26/09, Thomas Bätzler wrote: > From: Thomas Bätzler > Subject: AW: compact my wordlist generator > To: "begginers perl.org" > Cc: "Michael Alipio" > Date: Monday, October 26, 2009, 10:40 PM > Michael Alipio > wrote: > > Can anyone tell me how the code above works? My > original program must > > deal with arbitrary length and generate all the > possible combinations > > (even repeating) of a particular set. What could take > me gazillions of > > for loops for that, somebody just came up with less > than ten lines. > > I can just copy and paste the code I found above but I > want to learn how > > it works. How could someone write this code so easily > but when I tried > > even writing just a pseudocode for it, my head almost > exploded. > > It probably helps if you have some understanding of the > underlying mathematics of the problem domain, i.e. in this > case combinatorics. > > This would also clear up potential misunderstandings viz > the definition of a permutation ("randomly pick n of m > elements without putting them back") vs. that of a > combination ("randomly pick n of m elements while putting > them back"). > > > I want to be a good programmer, but at this rate, I > don't think I can be > > one if I will just keep copying and pasting someone > else's code or > > downloading modules from CPAN. > > Actually, taking somebody else's code and trying to figure > out how it works is a good exercise ;-) > > > Can anyone teach me how my own code (the one with > gazillion for loops), > > can be converted into a pseudocode then eventually > into a working sub > > procedure? > > No, probably not right away. But the good news is that most > people don't come up with elegant solutions off the top of > their heads - instead, they will have learned a number of > basic patterns and/or algorithms for solving common > problems, and they will have the experience to adapt what > they know to new problems. > > So, in order to become a better programmer you should look > at stuff people have been doing before you. Go to your local > library and borrow a book on Algorithms. Try to implement > them in Perl. Have a look at the Perl cookbook. If you want > a challenge, then treat yourself to a copy of "Higher Order > Perl" by Mark Jason Dominus. > > HTH, > Thomas > > -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: A Revised Logo for Perl
> "M" == "M E8 H " writes: M> This is an minor topic. I feel the Camel logo to represent Perl to be M> strange, illogical and slightly ugly. I presume I do not get the humor. In that sense, it represents Perl precisely. print "Just another Perl hacker,"; # the original! -- Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 http://www.stonehenge.com/merlyn/> Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc. See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: split n characters into n chunks
Dr.Ruud wrote: > Shawn H Corey wrote: > > >> push @list, (unpack( "A${i}A$size", $word ))[1]; > > Be careful with unpack "A", because it rtrims. > > > Best use "x" to skip, and "a" to capture. > > push @list, unpack "x${_}a$size", $word for 0 .. $max; > > > Funnily enough, that is somehow&what faster than > > push @list, map unpack( "x${_}a$size", $word ), 0 .. $max; > > You don't need the push: my @list = map unpack( "x${_}a$size", $word ), 0 .. $max; -- Just my 0.0002 million dollars worth, Shawn Programming is as much about organization and communication as it is about coding. I like Perl; it's the only language where you can bless your thingy. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
AW: compact my wordlist generator
Michael Alipio wrote: > Can anyone tell me how the code above works? My original program must > deal with arbitrary length and generate all the possible combinations > (even repeating) of a particular set. What could take me gazillions of > for loops for that, somebody just came up with less than ten lines. > I can just copy and paste the code I found above but I want to learn how > it works. How could someone write this code so easily but when I tried > even writing just a pseudocode for it, my head almost exploded. It probably helps if you have some understanding of the underlying mathematics of the problem domain, i.e. in this case combinatorics. This would also clear up potential misunderstandings viz the definition of a permutation ("randomly pick n of m elements without putting them back") vs. that of a combination ("randomly pick n of m elements while putting them back"). > I want to be a good programmer, but at this rate, I don't think I can be > one if I will just keep copying and pasting someone else's code or > downloading modules from CPAN. Actually, taking somebody else's code and trying to figure out how it works is a good exercise ;-) > Can anyone teach me how my own code (the one with gazillion for loops), > can be converted into a pseudocode then eventually into a working sub > procedure? No, probably not right away. But the good news is that most people don't come up with elegant solutions off the top of their heads - instead, they will have learned a number of basic patterns and/or algorithms for solving common problems, and they will have the experience to adapt what they know to new problems. So, in order to become a better programmer you should look at stuff people have been doing before you. Go to your local library and borrow a book on Algorithms. Try to implement them in Perl. Have a look at the Perl cookbook. If you want a challenge, then treat yourself to a copy of "Higher Order Perl" by Mark Jason Dominus. HTH, Thomas -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: A Revised Logo for Perl
Shawn H Corey wrote: > Perl 6 has a different logo, a cute little butterfly, awww... > http://perl6.org/ Which (AFAICR) is non-negotiable and in textual mode looks like this: »ö« :^) -- Raf http://www.catb.org/~esr/faqs/smart-questions.html -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: split n characters into n chunks
Shawn H Corey wrote: push @list, (unpack( "A${i}A$size", $word ))[1]; Be careful with unpack "A", because it rtrims. Best use "x" to skip, and "a" to capture. push @list, unpack "x${_}a$size", $word for 0 .. $max; Funnily enough, that is somehow&what faster than push @list, map unpack( "x${_}a$size", $word ), 0 .. $max; -- Ruud -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: compact my wordlist generator
hi, I have this code, mentioned below: my @word = qw(a b c ); my $length = @word; my $char1, $char2, $char3; if ($length == 3){ for ($char1 = 0; $char1<$len;$char1++){ for ($char2 = 0; $char2<$len;$char2++){ for ($char3 = 0; $char3<$len;$char3++){ print "$word[$char1]$word[$char2]$word[$char3]\n"; }}} }elsif($length == 2){ for ($char2 = 0; $char2<$len;$char2++){ for ($char3 = 0; $char3<$len;$char3++){ print "$word[$char1]$word[$char2]\n"; }elsif($length == n){ and so on... } Then as suggested (see far below), a recursive function will do what I want.. After some googling I found this: permutate(0,2); sub permutate($$){ my ($cur,$max) = @_; if ($cur>=$max){ print "$result\n"; return; } for(@word){ substr($result,$cur,1)=$_; perm($cur+1,$max); } } Can anyone tell me how the code above works? My original program must deal with arbitrary length and generate all the possible combinations (even repeating) of a particular set. What could take me gazillions of for loops for that, somebody just came up with less than ten lines. I can just copy and paste the code I found above but I want to learn how it works. How could someone write this code so easily but when I tried even writing just a pseudocode for it, my head almost exploded. I want to be a good programmer, but at this rate, I don't think I can be one if I will just keep copying and pasting someone else's code or downloading modules from CPAN. Can anyone teach me how my own code (the one with gazillion for loops), can be converted into a pseudocode then eventually into a working sub procedure? --- On Sun, 10/25/09, Gabor Szabo wrote: > > Hi, > > > > I'm trying to write a word list generator which can > > generate all possible combinations of n characters, > within n > > set of characters. > > > > > > So far, this is what I have come up. The only input > is the > > lenght of the password the user wants. > > > What about keeping the characters in an array @char > so you will have > $char[0], $char[1] etc. a > > nd the loops could be replaced by a recursive function > call. Something > like this: > > do_something_for_char($k) > > sub do_something_for_char { > my ($k) = @_; > return if $k >= $n; > do_something_for_char($n+1); > } > > > Gabor > -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: split n characters into n chunks
Shawn H Corey wrote: John W. Krahn wrote: $ perl -le' my $word = "thequickbrown"; my $subsets = 3; print for $word =~ /(?=(.{$subsets}))/g; Getting up there but substr is still the fastest. I had to set the iterations to 300_000, to get rid of warnings. $ perl5.8.8 3.pl Rate arrays match unpack match3 match2 unpack2 substr arrays 41265/s ---39%-40%-43%-44%-51%-73% match67114/s 63% -- -2% -8% -9%-20%-56% unpack 68337/s 66% 2% -- -6% -7%-19%-56% match3 72816/s 76% 8% 7% -- -1%-13%-53% match2 73350/s 78% 9% 7% 1% ---13%-52% unpack2 84034/s104% 25% 23% 15% 15% ---45% substr 153846/s273%129%125%111%110% 83% -- I moved some of the setup up, because I felt like it. unpack2() has less overhead than unpack. substr() mainly wins because it doesn't copy data. (I assume it just creates an extra SvP on (a part of) it) $ cat 3.pl #!/usr/bin/perl -w use strict; $| = 1; use Data::Dumper; # Make Data::Dumper pretty $Data::Dumper::Sortkeys = 1; $Data::Dumper::Indent = 1; # Set maximum depth for Data::Dumper, zero means unlimited $Data::Dumper::Maxdepth = 0; sub Testing() { 0 } use Benchmark qw(:all); my $word = "thequickbrown"; my $size = 3; Testing and print "$_$/" for my $re_match = sprintf( ".(?=%s)", "." x ($size-1) ), my $re_match2 = sprintf( "(?=(%s))", "." x $size ), ; my $max = length( $word ) - $size; if ( Testing ) { via_arrays(); via_substr(); via_unpack(); via_match(); via_match2(); via_unpack2(); } else { cmpthese( 300_000, { 'arrays' => \&via_arrays, 'substr' => \&via_substr, 'unpack' => \&via_unpack, 'unpack2' => \&via_unpack2, 'match' => \&via_match, 'match2' => \&via_match2, }); } sub via_arrays { my @list = (); my @array = split //, $word; push @list, join '', @array[ $_ .. $_ + $size - 1 ] for 0 .. $max; print Dumper \...@list if Testing; } sub via_substr { my @list = (); push @list, substr( $word, $_, $size ) for 0 .. $max; print Dumper \...@list if Testing; } sub via_unpack { my @list = (); push @list, (unpack( "A${_}A$size", $word ))[1] for 0 .. $max; print Dumper \...@list if Testing; } sub via_unpack2 { my @list = (); push @list, unpack( "x${_}a$size", $word ) for 0 .. $max; print Dumper \...@list if Testing; } sub via_match { my @list = (); push @list, substr( $word, $-[0], $size ) while $word =~ /$re_match/og; print Dumper \...@list if Testing; } sub via_match2 { my @list = (); push @list, $_ for $word =~ /$re_match2/og; print Dumper \...@list if Testing; } -- Ruud -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: AW: perl document
On Monday 26 Oct 2009 10:05:05 Thomas Bätzler wrote: > mahesh bhasme asked: > > I am new to perl. From where I get the basic perl document > > Depends on your distribution, really. If you're on Windows and using the > ActiveState Perl distribution, look for the html folder in your Perl > installation directory. > > On Unix, you might want to try perldoc or man to get you started. Have a > look at the perlsyn, perlop and perlrun documents ("perldoc persyn" etc) > if you've got some programming experience. > > If you don't, you might be better off with supplementary documentation in > book form like "Learning Perl" - please see http://books.perl.org/. > > HTH, > Thomas > As Thomas notes, Mahesh's request is phrased ambiguously. I should note that other people and I have concentrated links to most of the important Perl online resources here: http://perl-begin.org/ Any additions or corrections for it would be welcome. Regards, Shlomi Fish -- - Shlomi Fish http://www.shlomifish.org/ "The Human Hacking Field Guide" - http://shlom.in/hhfg Chuck Norris read the entire English Wikipedia in 24 hours. Twice. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: perl document
mahesh bhasme wrote: > Hi, > I am new to perl. From where I get the basic perl document > http://perldoc.perl.org/ -- Just my 0.0002 million dollars worth, Shawn Programming is as much about organization and communication as it is about coding. I like Perl; it's the only language where you can bless your thingy. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
AW: perl document
mahesh bhasme asked: > I am new to perl. From where I get the basic perl document Depends on your distribution, really. If you're on Windows and using the ActiveState Perl distribution, look for the html folder in your Perl installation directory. On Unix, you might want to try perldoc or man to get you started. Have a look at the perlsyn, perlop and perlrun documents ("perldoc persyn" etc) if you've got some programming experience. If you don't, you might be better off with supplementary documentation in book form like "Learning Perl" - please see http://books.perl.org/. HTH, Thomas -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
perl document
Hi, I am new to perl. From where I get the basic perl document -- Thanks, MAhesh
AW: Building a fmt line for printf with a carriage return
Hi, Wagner, David --- Senior Programmer Analyst --- CFS wrote: > Here is the sample script I was playing with: > #!/usr/bin/perl > > use strict; > use warnings; > > my $MyLine1 = q[%2d %5s %6s]; > my $MyLine2 = q[%2d %5s \n%6s]; The q// operator is equivalent to single quotes, so escape sequences like \n are not interpolated. If you want \n to be expanded to a newline character, you need to use qq or double quotes, or a combination of q// and qq// or quotes and text concatenation. HTH, Thomas -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/