Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Multidimensional arrays

by choroba (Cardinal)
on Jun 06, 2023 at 15:22 UTC ( [id://11152657]=note: print w/replies, xml ) Need Help??


in reply to Multidimensional arrays

Why do you want to create a sparse array? You don't want to iterate over it, you want to retrieve the details for a given plan. Use a hash of hashes instead.

Also, you probably don't need to repeat the "plan" and "length" in the inner hash, you already know them as they're the keys to it (unless you need to iterate over the values somewhere).

my %price = ( 1 => { 1 => { name => 'Early Adopter', price => 3.49, stripe => 'price_123245', }, 6 => { name => 'Early Adopter', price => 18.99, stripe => 'price_123267', }, 12 => { name => 'Early Adopter', price => 32.49, stripe => 'price_123289', }}, 2 => { 1 => { name => 'First Increase', price => 3.99, stripe => 'price_123445', }, ...
Update: BTW, the syntax you used is not correct.
Multidimensional syntax $price[1,1] not supported at 1.pl line 6.

The correct syntax is $price[1][1].

Similarly, the syntax for the hash of hashes will be

my $gbp_sum = $price{$plan}{$length}{price};

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: Multidimensional arrays
by Bod (Parson) on Jun 06, 2023 at 15:52 UTC

    Thanks choroba

    The correct syntax is $price[1][1]

    Can you tell I don't use multidimensional arrays very often?

    I suppose I was thinking of multidimensional hashes which I think have a different name...
    $hash{'dimension', 'type'};

      $hash{'dimension', 'type'};

      Yes, these exist and are equivalent to

      $hash{ join $;, qw( dimension type ) }
      Not something you should use normally. Probably not even worth remembering, but you already did.

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
        Not something you should use normally. Probably not even worth remembering, but you already did

        I remembered them because I have a bit of legacy code that produces them from an HTTP file upload...

        $file{$fileid, 'filename'}; $file{$fileid, 'content'};

        I don't use it for new stuff but there is quite a bit of existing code that still uses so I have to deal with it on occasion. I am guessing that's what I read that shouldn't be used now rather than multidimensional arrays.

      The multidimensional hashes were actually used for multidimensional array (or hash) emulation, before references were added in perl5 to make those and more complex structures far easier. The old style is still supported, but modern reference-based structures are almost always preferable: see $SUBSCRIPT_SEPARATOR.

      Also, there is nothing wrong with multidimensional arrays. In fact they are so useful, they even have their own manpage: perllol.

        Also, there is nothing wrong with multidimensional arrays

        Thanks hv.
        I feel I was thinking about multidimensional hashes when I recall reading that they shouldn't be used now. See my reply to choroba Re^4: Multidimensional arrays

Log In?
Username:
Password:

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

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

    No recent polls found