Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

passing bits of an array to an executable a piece at a time

by Anonymous Monk
on Aug 08, 2003 at 09:09 UTC ( #282140=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'd like to modify the code below so that I take the array $ids, hack it up into 200 bit chunks and pass it on to glim.exe that way. I can't seem to get it to work. Many thanks in advance. -Mark

# Global: $OS_TYPE trace_me(); my ($c) = @_; our ($OS_TYPE); my ($ids); $ids = selected_ids($c); if ($OS_TYPE eq 'mswin32') { # Win32 launch_background('glim.exe', 'glim ' . join(' ', @{$ids}));

20030808 Edit by Corion: Added CODE tags

Replies are listed 'Best First'.
Re: passing bits of an array to an executable a piece at a time
by BrowserUk (Patriarch) on Aug 08, 2003 at 12:10 UTC

    Whether this i sthe best way of doing this is an open question, but to pass your array in 200 element chucks, you could use splice. I'm assuming that select_ids() returns a reference to an array.

    launch_background( 'glim.exe', 'glim', join' ', splice( @$ids, 0, 200 ); ) while @$ids;

    This will consume the array. If you need to retain the array then make a copy. If the array is big and copying costs too much memory then you would need to use a slice and a pointer.

    launch_background( 'glim.exe', 'glim', join' ', @{ $ids }[ $_ .. ( $_ + 200 ) ] ) for map $_ * 200, 0 .. int( @$ids / 200 );

    You will probably need to add a little logic to the calculation of the upper bound of the slice to prevent "Uninitialised values" warnings unless your array is a multiple of 200.

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
    If I understand your problem, I can solve it! Of course, the same can be said for you.

Re: passing bits of an array to an executable a piece at a time
by krisahoch (Deacon) on Aug 08, 2003 at 11:18 UTC


    If you are passing these arguments to a console app, remember, you only have 255 characters to work with. This includes spaces and the name of the program. If you pass more than 255, your string will be truncated.

    Kristofer Hoch

    Quidquid latine dictum sit, altum sonatur.
      That certainly is a matter of which environment he runs it it. I'm quite sure there is also a great difference between the various windows versions.

      T I M T O W T D I


        The information that I posted regarding the console argument limit comes from the Microsoft OEM Preinstall SDK for Windows 98, and 2000. I don't remember if Windows ME had a different number.

        The OEM Preinstall SDK is used by companies like Compaq, and Dell where they install the operating system and other applications in an automated fashion.

        Kristofer Hoch

        With great power comes great respondsibility -- Ben Parker, Amazing Fantasy #15
Re: passing bits of an array to an executable a piece at a time
by Cine (Friar) on Aug 08, 2003 at 11:45 UTC
    Instead of parsing the your data on the argument list, you should consider opening a pipe to the opened program, so that it can just read the data from stdin. See Open2.

    T I M T O W T D I
Re: passing bits of an array to an executable a piece at a time
by Zeroth (Beadle) on Aug 08, 2003 at 09:47 UTC
    Could you give some more info on how launch_background() works?

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://282140]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (1)
As of 2022-06-26 05:27 GMT
Find Nodes?
    Voting Booth?
    My most frequent journeys are powered by:

    Results (83 votes). Check out past polls.