Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: Hash vs constant vs package vs other for data structure

by oldtechaa (Beadle)
on Mar 27, 2017 at 19:03 UTC ( [id://1186128]=note: print w/replies, xml ) Need Help??


in reply to Re: Hash vs constant vs package vs other for data structure
in thread Hash vs constant vs package vs other for data structure

I don't have methods, so I don't believe I'll go the OO route. It seems like the constant or uppercase methods perform better, are just as readable, and don't make me change my data structure. What are the advantages of the hash method?
  • Comment on Re^2: Hash vs constant vs package vs other for data structure

Replies are listed 'Best First'.
Re^3: Hash vs constant vs package vs other for data structure
by haukex (Archbishop) on Mar 27, 2017 at 19:09 UTC
    What are the advantages of the hash method?

    Two that I can think of off the top of my head are that you can have sparse property sets (e.g. if one object has {foo=>1,bar=>2} and the other has {quz=>1,baz=>2}, whereas you'd need four array elements to cover that, a bit of a waste), and that textual serializations of the data would be self-documenting. But if neither of those are a concern to you, then at the moment I can't think of major disadvantages to using constants for the array indicies.

      I don't really need either of those, but I also would like to assign to slices, which would be difficult with a hash. I think I'll go with the constant method or uppercase variables probably.
        Constants set up with constant are inlined by the compiler (Constant Functions) and should be a little bit faster, and "safer" (no one can accidentally change your variables).
        I also would like to assign to slices, which would be difficult with a hash.

        It works:

        $ perl -MData::Dump -e '@{$x[0][0]}{qw/a b/} = qw/c d/; dd \@x' [[{ a => "c", b => "d" }]]
        When faced with a choice which makes little difference, choose one and use it consistently on all future projects. (This restriction may influence your choice.)
        Bill
Re^3: Hash vs constant vs package vs other for data structure
by FreeBeerReekingMonk (Deacon) on Mar 27, 2017 at 21:14 UTC
    perl -MData::Dumper -E '$A[3][5][0]="foo"; die Dumper \@A'

    output:

    $VAR1 = [ undef, undef, undef, [ undef, undef, undef, undef, undef, [ 'foo' ] ] ];
    Long? Just try 300 by 500!

    perl -MData::Dumper -E '$A{3}{5}{0}="foo"; die Dumper \%A'

    output:

    $VAR1 = { '3' => { '5' => { '0' => 'foo' } } };

    smaller, but slower to iterate through if you have lots of entries. Still you can use an intermediate variable that acts like a pointer:

    perl -MData::Dumper -E '$A{3}{5}{0}="foo"; $v = $A{3}{5}; $v->{1}="bar +"; die Dumper \%A'

    if you have fixed dimensions... you can also try: $NUM = $x + $y*$WIDTH so if you have 300 pixels wide, (x,y)=(3,5) becomes 3+5*300 = 1503

    but check if you will not run above your maxint: 718414 with that method

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2024-04-18 10:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found