Interesting ... if I'm reading the OP correctly, you want to create a looping structure that is influenced in two ways by a command line argument: first, the number of loops is controlled by the length of the command line argument; and two, what you're looping is controlled by the command line argument.

So if the command line argument is CV, you loop as such:

foreach my $con ( @consonants ) { foreach my $vow ( @vowels ) { .. } }
and if the command line argument is CVCCVC, you loop as such:
foreach my $con_1 ( @consonants ) { foreach my $vow_1 ( @vowels ) { foreach my $con_2 ( @consonants ) { foreach my $con_3 ( @consonants ) { foreach my $vow_2 ( @vowels ) { foreach my $con_4 ( @consonants ) { ... } } } } } }
is that correct?

My first reaction is find another way. But if you wish to persist, I think recursion is your frenemy here:

#!/usr/bin/perl my $template = shift || die "usage: $0 <template>\n"; my @pattern = split( //, $template ); my $levels = scalar( @pattern ); my $vowels = [ qw( a e i o u y ) ]; my $consonants = [ qw( b c d f g h j k l m n p q r s t v w x z ) ]; output_cv( 0, \@pattern, $consonants, $vowels, "" ); sub output_cv { my( $level, $pattern, $consonants, $vowels, $string ) = @_; # if we have no more levels in the pattern, output the buffered stri +ng if( $level == scalar( @$pattern ) ) { print "$string\n"; } else { # figure out which array at this pattern level my $array = $pattern->[$level] eq 'C' ? $consonants : $vowels; foreach my $ele ( @$array ) { # start new buffer string my $new_string = $string . $ele; # recurse to next level my $new_level = $level + 1; output_cv( $new_level, $pattern, $consonants, $vowels, $new_stri +ng ); } } }

I say frenemy because the above is a fairly naive implementation and eventually some combination of template size and/or arrays is going to blow the stack.

-derby

In reply to Re: variables from STDIN by derby
in thread variables from STDIN by stigmatt

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.