Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Right, but wouldn't a deep copy mean that the nested stuff also gets copied, so it's even slower?

Yes, a deep copy would be even slower. When I said "The shallow copy would be a problem if your arrays are large", the emphasis was supposed to be on copy - in other words, it would be wasteful to make a copy of a large array, no matter whether it's a shallow or deep copy, but if the array is always small, making a copy may be acceptable (though as I said, I still don't recommend it).

And the fast way would be to make an alias, i.e. copy just the reference to the whole array? ... I'm still interested in learning about copying/aliasing though, even if it's not needed for this particular thing.

Note that "aliases" in Perl are distinct from references. There are ways to do aliasing, e.g. Data::Alias:

use warnings; use strict; use Data::Alias; use Data::Dump; my %animals = ( cat => [1,2,3], dog => [5,6], pig => [4,3,2,1], ); alias my @cat = @{ $animals{cat} }; $cat[1] = 'cat'; push @cat, 4; dd \%animals; __END__ { cat => [1, "cat", 3, 4], dog => [5, 6], pig => [4, 3, 2, 1] }

But note its documentation says "The core's aliasing facilities are implemented more robustly than this module and are better supported. If you can rely on having a sufficiently recent Perl version, you should prefer to use the core facility rather than use this module. If you are already using this module and are now using a sufficiently recent Perl, you should attempt to migrate to the core facility." which refers to the currently still experimental refaliasing I mentioned. That's why I suggested references, since those are fully supported everywhere and let you build complex data structures.

but then the array would be referred to as @$cat, which would be confusing in the rest of the code.

Yes, the reference would have to be dereferenced. Since nested data structures are pretty common, I'd suggest practicing using references so it's less confusing.

my @cat=(1,2,3); my @dog=(5,6); my @pig=(4,3,2,1); my %animals; $animals{'cat'}=\@cat; $animals{'dog'}=\@dog; $animals{'pig'}=\@pig;
Slightly wordy, but fulfils all the criteria: No symbolic references, no globals, no copying, and @cat etc. look like they're supposed to for the remaining code. Right?

Note that this code does pretty much the same as the code in my first reply, with the exception that this also creates the three arrays as lexicals. But otherwise yes, this is one way to do it.

\@{$animals{'cat'}} ... ${$animals{$_}}[1]

Note that these two are written more simply as $animals{cat} and $animals{$_}[1], respectively. I would strongly recommend a read of perlreftut, perlref, and perldsc.


In reply to Re^8: Referencing the locals by haukex
in thread Referencing the locals by Chuma

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-20 04:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found