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

Re: Map a string to an array

by runrig (Abbot)
on Nov 01, 2013 at 22:36 UTC ( #1060848=note: print w/replies, xml ) Need Help??

in reply to Map a string to an array

@val = $mapp{"FirstKey"};
This assigns a reference to @arr to $val[0], because $mapp{FirstKey} contains a reference to the array @arr. When you stringify the reference, you get something like "ARRAY(0x7ffafc013bf8)". If you wanted to make @val a copy of @arr, then you can:
@val = @{$mapp{FirstKey}};

Replies are listed 'Best First'.
Re^2: Map a string to an array
by jess195 (Novice) on Nov 01, 2013 at 22:41 UTC

    What would be the case if this is a 2d array? i.e, the map is from a string to 2d array.

    Is there an easy way to print the content?

      Not sure what you're asking. Do you want to make a copy of a 2D array? Or if you don't really need a copy, can you just use the reference.
      my @arr = ( [1,2], [3,4], ); my %hash = ( FirstKey => \@arr ); my $aref = $hash{FirstKey}; my $row0 = $aref->[0]; push @$row0, 99; use Data::Dumper; print Dumper \%hash;

      Further to runrig's replies:
      It's important to understand at each step how the structures of the hash and arrays are changing and evolving. Here's a kind of "running commentary" on that evolution, and a possible way to access and print what you want. Liberal use of data structure (for that's what you're dealing with; see perldsc) dump statements can be very enlightening about the changes that are going on. (And BTW, congratulations on being able to work with a system having at least 128 Terabytes of system RAM — or do I misinterpret the reference address of 0x7ffafc013bf8?)

      >perl -wMstrict -MData::Dump -le "my %mapp; my @arr = qw(R T HH M); ;; $mapp{k} = \@arr; my @val = $mapp{k}; ;; dd \%mapp; dd \@val; ;; print join q{,}, @{$mapp{k}}; ;; push @val, 'RBB'; dd \@val; ;; $mapp{k} = \@val; dd \%mapp; ;; print join q{,}, @{$mapp{k}}; print join q{,}, @{ $mapp{k}->[0] }, $mapp{k}->[1]; " { k => ["R", "T", "HH", "M"] } [["R", "T", "HH", "M"]] R,T,HH,M [["R", "T", "HH", "M"], "RBB"] { k => [["R", "T", "HH", "M"], "RBB"] } ARRAY(0x1e14b3c),RBB R,T,HH,M,RBB
        (And BTW, congratulations on being able to work with a system having at least 128 Terabytes of system RAM or do I misinterpret the reference address of 0x7ffafc013bf8?)

        With virtual memory, heap does not have to be contiguous.

        This C program allocates a single page of memory starting at the 4GB base address and then doubles the base address each time until it fails:

        #define _CRT_SECURE_NO_WARNINGS #include <windows.h> #include <conio.h> #include <time.h> #include "debug.h" #include "mytypes.h" int main( int argc, char **argv ) { U64 size = 4096, base = 4294967296; char *p; while( 1 ) { char *p = VirtualAllocEx( GetCurrentProcess(), (VOID*)base, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE ); DIEIF( p == NULL ); printf( "4k allocated at:%16p (%30I64d)\n", p, 0+p ); _getch(); base *= 2; } exit( 0 ); }

        I only have 8GB of ram, but the last time the above succeeds:

        C:\test>vmem3 4k allocated at:0000000100000000 ( 4294967296) 4k allocated at:0000000200000000 ( 8589934592) 4k allocated at:0000000400000000 ( 17179869184) 4k allocated at:0000000800000000 ( 34359738368) 4k allocated at:0000001000000000 ( 68719476736) 4k allocated at:0000002000000000 ( 137438953472) 4k allocated at:0000004000000000 ( 274877906944) 4k allocated at:0000008000000000 ( 549755813888) 4k allocated at:0000010000000000 ( 1099511627776) 4k allocated at:0000020000000000 ( 2199023255552) 4k allocated at:0000040000000000 ( 4398046511104) [ 1916] vmem3.c(18):p == NULL failed with error 87 (The parameter is incorrect)

        It allocated a page at a base address that is 4096 GB. At that point the process is only using 668k of memory.

        4096GB is 2^42 which reflects that my 64-bit processor actually only has a 44-bit virtual address space.

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1060848]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2018-03-17 15:03 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (224 votes). Check out past polls.