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

Re: How to create a new array with the same name as an existing one in a loop?

by haj (Vicar)
on Aug 15, 2021 at 18:06 UTC ( [id://11135859]=note: print w/replies, xml ) Need Help??


in reply to How to create a new array with the same name as an existing one in a loop?

Just a quick guess: Create a copy to your list instead of a reference:

$current_main_record{"role_data"} = [@role_records];

Replies are listed 'Best First'.
Re^2: How to create a new array with the same name as an existing one in a loop?
by AnomalousMonk (Archbishop) on Aug 15, 2021 at 18:12 UTC
    ... = [@role_records];

    MrSnrub: Note that this is a shallow copy.


    Give a man a fish:  <%-{-{-{-<

      how do you do a deep copy? i want to be able to copy everything.
        i want to be able to copy everything.

        Why? Why not just create a new array reference each time through the loop? Again, I don't think we have a good idea of your true requirement. Please see Short, Self-Contained, Correct Example.

        Update: In any event, searching with "deep copy" on MetaCPAN brings up Clone (update: and Clone::PP), about which I know nothing.


        Give a man a fish:  <%-{-{-{-<

        There are various modules offering a "clone" routine, even in core.

        But the question is tricky because details are important if blessed objects ° are involved or speed matters.

        In the end you need to know what you really want.

        Update

        °) To be more specific how do you clone an arbitrary object "correctly" if the class doesn't provide a clone method?

        And what is "correctly" and "good enough" ? :)

        Update

        For instance an array @bus containing objects of persons sitting in a bus like say $Rolf and $MrSnrub.

        After copying the bus, it'll depend on the application if you really want to clone the persons too.

        I'd consider creating a clone of person highly unwanted. Especially identity theft of $Rolf. ;)

        If I'm the passenger in two different busses, you'd want to update personal information like addresses only once...

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

        «…i want to be able to copy everything…»

        Probably this is possible:

        use MyClass; use Storable qw(dclone); my $object = MyClass->new(); my $clone = dclone($object); # NB.: Untested

        But I doubt that you really want this.

        «The Crux of the Biscuit is the Apostrophe»

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-03-29 14:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found