Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Peculiar hash behavior

by haukex (Archbishop)
on Oct 02, 2021 at 06:59 UTC ( [id://11137195]=note: print w/replies, xml ) Need Help??


in reply to Peculiar hash behavior

What you are seeing is the effect of Multi dimensional array emulation combined with the pitfalls of Data::Dumper's default settings that jwkrahn pointed out. You may also be interested in Data::Dump or my emulation of it here.

Replies are listed 'Best First'.
Re^2: Peculiar hash behavior
by dd-b (Monk) on Oct 02, 2021 at 18:08 UTC

    The array emulation is another one of those things I've read about a few times and never really understood well enough to be safe around :-) .

    I've been wondering if Data::Dumper is getting on towards its freshness date; I've played with alternatives a bit. Simpler (Data::Dump) does seem to be what's needed, and the ddx function is nicely adapted for debug use, as it says was intended. There do seem to be packages for the environments I care about (FreeBSD, Ubuntu, Raspberry Pi).

      I've been wondering if Data::Dumper is getting on towards its freshness date

      Data::Dumper has been maintained by P5P alongside the core, so it's unlikely to go stale. It's just a bit unfortunate (IMHO) that its Useqq option is off by default - the other options I use in the linked post are mostly for "nicer" output, so as long as you write use Data::Dumper; $Data::Dumper::Useqq=1;, the module is fine. Data::Dump has prettier output by default, but if you look at its issue list you'll see it's not perfect either. There are lots of other dumper modules out there. The main things to remember are that you want the modules to give unambiguous output (hence Useqq) and that these modules are debugging aids and not data serialization tools.

      > The array emulation is another one of those things I've read about a few times and never really understood well enough to be safe around :-) .

      I think it helps to know that Perl4 had no references˛, so none of the - now normal - nested data-structures described in perldsc were available.

      With the introduction of Perl5 multi-dim hashes had to be kept for backwards compatibility, but are for most an unknown or obscure feature.

      But they are quite handy sometimes, you'd be surprised how many people tend to reinvent something like $h{ join(",",@dims) }= "x" which is essentially the same without a safer separator.°

      HTH! :)

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

      °) a former boss was particularly proud to show me his "genius" trick to safe memory ...

      ˛) a lot had to be achieved with *type-globs (yuck)

        I remember the transition from Perl 3 to Perl 4, yeah. Lots of really useful new features.

      The array emulation is another one of those things I've read about a few times and never really understood well enough to be safe around :-) .

      $h{$x, $y, ...}
      is short for
      $h{join $;, $x, $y, ...}

      That's it.

      As long as your keys don't contain the value in $; (which is a control character by default), you can emulate multi-level hashes. It presumably uses less memory, but it might be a bit slower from concatenating the keys??? Of course, you're relying on the value of $; being safe.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-24 21:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found