Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

De-Reference an array

by Ankit.11nov (Acolyte)
on Oct 09, 2009 at 13:36 UTC ( #800266=perlquestion: print w/ replies, xml ) Need Help??
Ankit.11nov has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
Below is my piece of code,there are 2 perl scripts here. In define.pl I am declaring two arrays and then referencing them. Then I am passing these as a parameter to another perl script print.pl to print after de-referencing it

On running define.pl nothing is getting printed

File 1: Define.pl
use warnings; @array1=(1..3); @array2=(4..6); $ref_array1=\@array1; $ref_array2=\@array2; system("print.pl",$ref_array1,$ref_array2);

Print.pl
use warnings; ($ref_arr1,$ref_arr2)=@ARGV; print @$ref_arr1; print @$ref_arr2;
Can anyone please help me on this?

Comment on De-Reference an array
Select or Download Code
Re: De-Reference an array
by Anonymous Monk on Oct 09, 2009 at 13:41 UTC
    Then I am passing these as a parameter to another perl script print.pl to print after de-referencing it

    No you're not. You can only pass strings via ARGV

      Is there any other way of doing this? Also If I am passing a scalar varaible and an array from define.pl then it works fine. Only when I am passing the reference parameters it is giving this problem.
Re: De-Reference an array
by JavaFan (Canon) on Oct 09, 2009 at 13:46 UTC
    All you're passing are the stringified references. If you want to pass entire datastructures on the command line, use a serializer, and pass the serialized data on the command line. Deserialize in the called program.

      Can you please explain in more I didnt understood what do you mean by "All you're passing are the stringified references. If you want to pass entire datastructures on the command line, use a serializer, and pass the serialized data on the command line. Deserialize in the called program."

      Can you please modify the code to help me understand this.

        To understand what was meant by "All you're passing are the stringified references", change your code like this:

        #!/usr/bin/perl # program.pl use warnings; use strict; my @array1 =( 1..3 ); my @array2 = ( 4..6 ); my $ref_array1 = \@array1; my $ref_array2 = \@array2; system("./print.pl",$ref_array1,$ref_array2);

        ...and the print.pl:

        #!/usr/bin/perl use strict; use warnings; my ( $ref_string1 ,$ref_string2 ) = @ARGV; print "$ref_string1, $ref_string2\n";

        ...and run it.

        You can use 'Storable' to save your data serialized into a file, and then have the print program lift it back up:

        #!/usr/bin/perl use warnings; use strict; use Storable; my @array1 = ( 1..3 ); my @array2 = ( 4..6 ); my $store_file = './data.store'; # encapsulte the two arrays into a single array ref # for storage. We'll extract in print.pl store( [ \@array1, \@array2 ], $store_file ); # instead of passing the data as the parameters, pass the file name in +stead system( "./print.pl", $store_file );

        ...and the print....

        #!/usr/bin/perl use strict; use warnings; use Storable; my $store_file = $ARGV[0]; # we have to extract the two original arrays from the # single array that we encapsulated them in my $aoa = retrieve( $store_file ); my ( $aref1 ,$aref2 ) = @{ $aoa }; print "@{ $aref1 }, @{ $aref2 }\n";

        Hope this helps!

        Steve

        Change
        system("print.pl",$ref_array1,$ref_array2);
        to
        print "Array 1: $ref_array1\nArray 2: $ref_array2\n";
        And run, you'll see what you're passing to the second script.

        A suggested you'll have to provide the contents of the arrays rather than references to the second script.

Re: De-Reference an array
by bv (Friar) on Oct 09, 2009 at 14:27 UTC

    I'm not sure you really want to do what you think you are doing. Your code would be much cleaner, faster, and do what you mean if you kept it all in one script and used subs, like so:

    use strict; use warnings; my @array1 = ( 1 .. 3 ); my @array2 = ( 4 .. 6 ); print2arrs( \@array1, \@array2 ); sub print2arrs { my ( $ref1, $ref2 ) = @_; print @$ref1; print @$ref2; }

    And if you really do need to keep them in separate files, check out require or do to avoid using the system command and firing off an entirely new perl interpreter

    print pack("A25",pack("V*",map{1919242272+$_}(34481450,-49737472,6228,0,-285028276,6979,-1380265972)))
      Hi Steve, Thanks for the piece of code.

      What I am working upon: 1) Parsing ~100 of XML files 2) The information which is extracted is kept in an different arrays 3) Info available in the arrays is entered into an excel sheet.

      Thats what I was exploring with this small piece of code.
Re: De-Reference an array
by vitoco (Pilgrim) on Oct 09, 2009 at 14:47 UTC

    I guess you think that your array references are some kind of pointers to memory locations, and you want to access that memory structures from another program.

    Even if you could pass that memory addresses to the second program, I'm not sure you'll succeed, I think that OS will prevent that.

    Off-Topic: I remember doing something like that successfully on an old multiuser machine years ago. I forced a logout of another user by clearing he's assigned memory area using Pascal.  >:-)

      Shaing memory could be done by using IPC::Shareable and passing the assigned '$glue' to the 'print.pl' (or whatever) script.

      Just a something something...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (12)
As of 2014-07-25 09:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (170 votes), past polls