Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re (2): Array for system() call

by dmmiller2k (Chaplain)
on Oct 23, 2001 at 22:09 UTC ( #120855=note: print w/ replies, xml ) Need Help??


in reply to Re: Array for system() call
in thread Array for system() call

To elaborate further, use this (note double quotes): system("@Call"); instead of this: system(@Call); to achieve the same effect as your first fragment.

And, BTW, you probably meant to do this:

$Call[0] = "gawk"; $Call[1] = "-f"; $Call[2] = "test.gawk"; $Call[3] = "test.file"; $Call[4] = ">"; $Call[5] = "out.file";

or (slightly more efficiently) this:

@Call = ( "gawk", "-f", "test.gawk", "test.file", ">", "out.file" );

rather than your original code, which is assigning values to 1-element array slices, which are sublists -- NOT arrays! -- of references to the elements of the @Call array; needless to say, although it works, it is somewhat less efficient than either of the above approaches, and bad practice besides.

dmm

Give a man a fish and you feed him for a day ...
Teach the man to fish and you feed him for a lifetime


Comment on Re (2): Array for system() call
Select or Download Code
Re: Re (2): Array for system() call
by blakem (Monsignor) on Oct 23, 2001 at 22:45 UTC
    Here is an example of why using @arr[0] is just asking from trouble...
    #!/usr/bin/perl -wT use strict; my @arr; @arr[0] = localtime(); $arr[1] = localtime(); print "\$arr[0] = $arr[0]\n"; print "\$arr[1] = $arr[1]\n";
    Any guesses about the output of this simple script? Select area below to find out.

    =OUTPUT Scalar value @arr[0] better written as $arr[0] at ./arrayslice.pl line + 6. $arr[0] = 58 $arr[1] = Tue Oct 23 14:30:58 2001
    5 points for anyone who expected $arr[0] to contain an integer. 20 points for anyone who knew it would contain the seconds value, *not* the number of elements in the list returned by localtime().

    (10 points for anyone who coded up an example because they weren't sure... woohoo)

    -Blake

      Thanks for the example, blakem

      dmm

      You can give a man a fish and feed him for a day ...
      Or, you can teach him to fish and feed him for a lifetime
      

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (18)
As of 2015-07-06 14:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (75 votes), past polls