Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

sending a scalar to the interpreter as a command

by silentq (Novice)
on Oct 08, 2012 at 20:41 UTC ( #997874=perlquestion: print w/ replies, xml ) Need Help??
silentq has asked for the wisdom of the Perl Monks concerning the following question:

I am attempting to build a Perl statement which I want to run as a command, storing it in a variable as I go. The reason I want to do this is that I am dealing with arrays of varying sizes, and I can't think of a way to structure the command such that it will work with any size of array. So I am building a customized version of this command in the script each time it runs so that it will fit the size of any array it encounters, storing the command in my variable, and then attempting to route it to the interpreter. But with no luck.

I seem to remember when I first learned Perl years ago that there are ways to send variables to the interpreter as commands just as you can to the system using backtick-style quotes. Can anyone help?

Thanks,

PS here is a sample output of the command for anyone who is curious:

" @{$array[0]}, @{$array[1]}, @{$array[2]}, @{$array[3]}" =~ / (\S+) . +*?,.*? (\S+) .*?,.*? (\S+) .*?,.*? (\S+) (?{ push @res,"$1-$2-$3-$4"; + })(?!)/; print join ', ',@res;
PS I am escaping items as necessary, so that's not the problem.

PPS This is based on code I found elsewhere on the forum:

http://perlmonks.org/index.pl?node_id=662212

Comment on sending a scalar to the interpreter as a command
Download Code
Re: sending a scalar to the interpreter as a command
by davido (Archbishop) on Oct 08, 2012 at 21:08 UTC

    there are ways to send variables to the interpreter as commands

    Yes. eval (the quotish version).

    my $code = 'print qq{Hello world!\n};'; eval $code;

    Or, since the value returned is the value of the last expression evaluated:

    my $code = '1+1'; my $result = eval $code; print $result, "\n";

    I feel this answer, while probably accurate, could be a sub-optimal solution to your real problem. Many languages that are considered quite powerful provide no equivalent to eval, and the odds are good that most of those languages could solve the problem you're working on without eval. It might be the case that if you back up a step or two, and present the actual problem you're solving, rather than how you think you would like to solve it, you might get a better answer. eval is useful, but it's one of those things that usually can be avoided for the better.


    Dave

      Thanks for your quick response, David. I really appreciate it.

      I would actually prefer to solve the problem without resorting to eval just as you have recommended. But, for some reason, I just can't quite wrap my head around how to do it. Maybe you or someone else can help.

      What I have is a two-dimensional array filled with scores. That looks something like this:

      67 42 99 28 15 14 92 12 45 57 16 41 99 67 28 15 57 12 45 14 92 12 92 42 67 28 15 45 57 16 12 99 14

      The array can be varying widths and heights. In other words, there are variable numbers of rows and columns.

      What I want to do is find all the possible combinations when taking one item from each row. I believe there are x^y possible combinations where x represents the number of rows and y represents the number of columns. What I want is this: 67-41-92, 67-41-42, 67-41-67, 67-41-28 ...

      I believe it's just a matter of nesting arrays. The trick is setting up a dynamic number of these. I played around with the idea of using a while loop, but the problem is that this resulted in consecutive iterations through the arrays, not nested iterations.

      So I'm really at a loss at this point. If anyone can help, I'd be very grateful.

      Thanks,

        #! perl -slw use strict; sub loops { map { my $n = $_; @_ ? map{ $n . '-' . $_ } loops( @_ ) : $n; } @{ shift() }; } my @arrays = map [ split ], <DATA>; print for loops( @arrays ); __DATA__ 67 42 99 28 15 14 92 12 45 57 16 41 99 67 28 15 57 12 45 14 92 12 92 42 67 28 15 45 57 16 12 99 14

        Produces:

        67-41-92 67-41-42 67-41-67 67-41-28 67-41-15 67-41-45 67-41-57 67-41-16 67-41-12 67-41-99 67-41-14 67-99-92 67-99-42 67-99-67 ... 16-92-12 16-92-99 16-92-14 16-12-92 16-12-42 16-12-67 16-12-28 16-12-15 16-12-45 16-12-57 16-12-16 16-12-12 16-12-99 16-12-14

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        RIP Neil Armstrong

        What I want is this: 67-41-92, 67-41-42, 67-41-67, 67-41-28 ...

        glob can help with this:

        use strict; use warnings; my $a = join ',', qw/67 42 99 28 15 14 92 12 45 57 16/; my $b = join ',', qw/41 99 67 28 15 57 12 45 14 92 12/; my $c = join ',', qw/92 42 67 28 15 45 57 16 12 99 14/; print "$_\n" for glob "{$a}-{$b}-{$c}";

        Output:

        67-41-92 67-41-42 67-41-67 67-41-28 ... 16-12-16 16-12-12 16-12-99 16-12-14

        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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://997874]
Approved by Old_Gray_Bear
Front-paged by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2014-09-18 03:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (105 votes), past polls