Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^3: sending a scalar to the interpreter as a command

by kcott (Abbot)
on Oct 09, 2012 at 06:42 UTC ( #997933=note: print w/ replies, xml ) Need Help??


in reply to Re^2: sending a scalar to the interpreter as a command
in thread sending a scalar to the interpreter as a command

G'day silentq,

Welcome to the monastery.

Extending ++Kenosis' excellent idea of using glob, here's a solution for variable rows and columns.

#!/usr/bin/env perl use 5.010; use strict; use warnings; say for glob '{' . join('}-{' => map { join ',' => split } <DATA>) . ' +}'; __DATA__ 1 2 3 4 5 6 7 8 9

Output:

$ pm_2d_comb.pl 1-3-6-8 1-3-6-9 1-3-7-8 1-3-7-9 1-4-6-8 1-4-6-9 1-4-7-8 1-4-7-9 1-5-6-8 1-5-6-9 1-5-7-8 1-5-7-9 2-3-6-8 2-3-6-9 2-3-7-8 2-3-7-9 2-4-6-8 2-4-6-9 2-4-7-8 2-4-7-9 2-5-6-8 2-5-6-9 2-5-7-8 2-5-7-9

-- Ken


Comment on Re^3: sending a scalar to the interpreter as a command
Select or Download Code
Re^4: sending a scalar to the interpreter as a command
by silentq (Novice) on Oct 12, 2012 at 16:12 UTC

    Thanks very much for your responses, guys. I've learned a lot, but I don't think I'm quite there yet. I took Kenosis's code and turned it into this:

    use strict; use warnings; my @array = ([0,.95,.114,0], [1,0,0,1], [1,3,5,7]); my $var; my @a; for (my $i = 0; $i <= $#array; $i++) { $a[$i] = join ',', @{$array[$i]}; $var .= "\{\$a\[$i\]\}-"; # can't use singl +e quotes here because $i won't resolve } #my $a = join ',', @{$array[0]}; #print "$_\n" for glob "{$a[0]}-{$a[1]}-{$a[2]}"; #this line works, + but it's not dynamic, ie. always prints out exactly three lines print "$_\n" for glob $var; #this line doesn' +t work; apparently you can't resolve a variable to glob (?) print "$var\n\n"; #this line shows +what the $var variable is passing to glob in line 17

    The 'for' loop works fine for the first step, but I get tripped up on the second step. I thought I might be able to push the new reference onto a variable each time through the loop and then pass that variable to glob, but I tried it with and without quotes, and it doesn't seem to work.

    As for kcott's idea, try as I might, I cannot parse the following line of code:

    say for glob '{' . join('}-{' => map { join ',' => split } <DATA>) . ' +}';

    I understand what join does, and I also understand map and split. But the way they are combined here is a little bit beyond my ability to comprehend. For example, where is the second argument for the first join?

    In any case, the problem that remains seems to be getting each row of the array together into one glob statement. If I could do that, the modified Kenosis code above would work, I think.

    Thanks again to everybody for the time you have taken to help me try to get this resolved.

      Here's pm_2d_comb_long.pl - a longer, annotated version of pm_2d_comb.pl:

      #!/usr/bin/env perl use 5.010; use strict; use warnings; # Original code #say for glob '{' . join('}-{' => map { join ',' => split } <DATA>) . +'}'; # Read raw data # @data_lines will be: ('1 2', '3 4 5', '6 7', '8 9') my @data_lines = <DATA>; # Convert spaces to commas by splitting each string on whitespace # to form a list whose elements are joined with commas # @spaces_to_commas will be: ('1,2', '3,4,5', '6,7', '8,9') my @spaces_to_commas = map { join ',' => split } @data_lines; # Join those strings with '}-{' # $brace_dash_brace_string will be '1,2}-{3,4,5}-{6,7}-{8,9' my $brace_dash_brace_string = join('}-{' => @spaces_to_commas); # Add opening and closing braces # $glob_string will be: '{1,2}-{3,4,5}-{6,7}-{8,9}' my $glob_string = '{' . $brace_dash_brace_string . '}'; # Finally glob '{1,2}-{3,4,5}-{6,7}-{8,9}' say for glob $glob_string; __DATA__ 1 2 3 4 5 6 7 8 9

      Outputs:

      $ pm_2d_comb_long.pl 1-3-6-8 1-3-6-9 1-3-7-8 1-3-7-9 1-4-6-8 1-4-6-9 1-4-7-8 1-4-7-9 1-5-6-8 1-5-6-9 1-5-7-8 1-5-7-9 2-3-6-8 2-3-6-9 2-3-7-8 2-3-7-9 2-4-6-8 2-4-6-9 2-4-7-8 2-4-7-9 2-5-6-8 2-5-6-9 2-5-7-8 2-5-7-9

      -- Ken

        This helps a ton, kcott! I've been using Perl for a long time, but my focus is mostly on string processing and regular expressions. It's high time I got an education in other areas, and I feel like I've gotten a good start in this thread.

        Thanks so much to you and the others who have lent a hand to help me work through this problem.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://997933]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2014-07-12 19:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (241 votes), past polls