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

subroutine - Passing Array Reference by Reference

by Anonymous Monk
on Oct 30, 2005 at 03:17 UTC ( [id://503948]=perlquestion: print w/replies, xml ) Need Help??

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

I have an Array Reference "$arrayRef" and I want to pass it to a subroutine by reference.

How is this possible?

I've tried &sub(\$arrayRef); but it's not working. I'm still trying to come to grips with data structures but this is really blowing my mind. I want to pass by reference so I have no chance of harming my origional data.

Replies are listed 'Best First'.
Re: subroutine - Passing Array Reference by Reference
by friedo (Prior) on Oct 30, 2005 at 03:32 UTC
    I want to pass by reference so I have no chance of harming my origional data.

    I think you're a little confused about what passing-by-reference entails. You can easily take a reference to a reference -- you were correct about how to do that ( \$arrayRef ). If $refref is a reference to a reference to an array, then you can get the original array with @$$refref. But this does nothing to protect your original data. You're just going through an extra dereferencing step. If you want to make a copy of your original data, do a super-search for "deep copy." There are lots of methods depending on your needs.

Re: subroutine - Passing Array Reference by Reference
by Zaxo (Archbishop) on Oct 30, 2005 at 03:52 UTC

    This will pass a reference to a new copy of the original reference's data ("sub" is a really bad name for a sub, so I'll call it "stub"):

    stub([@{$arrayRef}]);
    That only copies down one level, so if $arrayRef is an AoA or something you'll need to adapt a deeper copy for true isolation;
    stub([map {[@$_]} @$arrayRef]);
    for an AoA, or
    stub([map {{%$_}} @$arrayRef]);
    for an AoH, for instance.

    After Compline,
    Zaxo

      Can you guys reccomend any good books that go indepth on references?
        "Learning Perl Objects, References & Modules", O'Reilly Publ.
        chas
Re: subroutine - Passing Array Reference by Reference
by pg (Canon) on Oct 30, 2005 at 03:51 UTC

    In your case, if someone says passing by reference, it usually means:

    foo($arrayRef);

    As $arrayRef is a reference already. In the same sense, if you have @array, then you:

    foo(\@array);

    If you don't want to pass by reference, you just:

    foo(@array);

      Maybe offtopic but keep in mind that when passing more than one parameter the parameter list will be flattened. You do need references in that case to distinguish between the items in your parameter list!

      The following code doesn't consider the two different arrays, it only sees one long parameter list.

      #! /usr/bin/perl use strict; use warnings; my @array1 = ( 1, 2, 3, 4, 5); my @array2 = ( 1, 2, 3, 4, 5); list(@array1,@array2); sub list { foreach my $c (@_) { print $c . " "; } }

      The following code only expects references and will treat each reference as an array reference.

      #! /usr/bin/perl use strict; use warnings; my @array1 = ( 1, 2, 3, 4, 5); my @array2 = ( 1, 2, 3, 4, 5); list(\@array1,\@array2); sub list { foreach my $r (@_) { foreach my $c (@$r) { print $c . " "; } print "\n"; } }
      --
      if ( 1 ) { $postman->ring() for (1..2); }
Re: subroutine - Passing Array Reference by Reference
by Cristoforo (Curate) on Oct 30, 2005 at 05:53 UTC
Re: subroutine - Passing Array Reference by Reference
by planetscape (Chancellor) on Oct 30, 2005 at 11:56 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (1)
As of 2024-04-25 03:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found