0

This is what I want.

my %h;

my @l = ("a", "b", "c");
$h {A} = \@l;

my @l = ("d", "e", "f");   # @l already declared: warning
$h {B} = \@l;

print Dumper (%h);

But I get a warning because of the double my of @l.

How can I re-init @l with keeping the former memory allocated?

Yes, I could enclose it within a block and the warning would be gone.

{
 my @l = ("a", "b", "c");
 $h {A} = \@l;
}

Yes, I could do it without an array variable.

$h {A} = ("a", "b", "c");

But is there another way too? I want to have @l and no blocks.

4
  • 2
    What do you mean with: I want to have @l and no blocks. ?
    – Wolf
    Feb 6, 2021 at 8:32
  • 2
    Updated my answer; please clarify things a little? :)
    – zdim
    Feb 6, 2021 at 8:58
  • 1
    (like, how is this @l used and where it comes from etc, in reality)
    – zdim
    Feb 6, 2021 at 9:07
  • The word blocks suggests that you want to avoid memory fragmentation, I added information about that (caution! this may be shocking).
    – Wolf
    Feb 6, 2021 at 9:27

2 Answers 2

4

Not sure exactly what you want, but what seems to me to be asked isn't possible.

An example

perl -wE'my @ary = (1..3); my %h = ( A => \@ary ); say $h{A}'

This prints ARRAY(0x25cc888) (on my system right now). That's the address of @ary in there.

So when you change the values in the array -- whichever way you do it -- your $h{A} will still contain the same address, and so when dereferenced it'll print out the new values. And new keys assigned this same array reference will then point to same data.

One way to use an array variable to populate distinct hash values is by copying data

perl -wE'
    my @ary = (1..3); 
    my %h = ( A => [ @ary ] ); 
    say for @{$h{A}};
    @ary = (10);      
    say for @{$h{A}}'   # stayed the same

Here we construct an anonymous array reference using [ ], into which the array values are copied. So when the @ary values change then the values referred to by $h{A} address don't since htey have nothing to do with @ary.

Then you can keep assigning data into the same array variable and use it to populate a data structure by copying them as shown above

my @l = ("a", "b", "c");
$h {A} = [ @l ];

@l = ("d", "e", "f");   # no need to redeclare
$h {B} = [ @l ];

or perhaps rather something like

# @data used before/after so can't declare within scope
while (@data = < $some_data_source >) {
    ...
    $h{ $some_label } = [ @data ];
    ...
}

(the question specifically asks to not use a separate scope)

2
  • @Wolf "isn't more effective" -- You mean efficient? No, of course not; on the contrary, and we usually avoid data copy like this. And it isn't clear to me what they want but they seem to want to use one array variable, what surely has good use cases. (Perhaps they keep receiving data from somewhere into an array and want to populate a large data structure, for example...)
    – zdim
    Feb 6, 2021 at 8:38
  • 1
    Yes, sorry "efficient" is the right word. To me the word "blocks" mentioned in the last sentence and a look on the profile page ([c] language) suggests that OP wants to keep control over the memory layout. And that makes - in my opinion - also no sense in Perl.
    – Wolf
    Feb 6, 2021 at 8:42
3

Main issue

What you are trying to achieve seems illogical to me, the statements about your intention seem to contradict each other: Re-initialization of the same array memory to populate a hash with values that are references to that same array. For different values you need different memory.

Assuming this is what you want your hash to contain,

$VAR1 = {
          'A' => [
                   'a',
                   'b',
                   'c'
                 ],
          'B' => [
                   'd',
                   'e',
                   'f'
                 ]
        };

...what's wrong with doing it like this?

#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper qw(Dumper);

my %h;
$h{A} = ["a", "b", "c"];
$h{B} = ["d", "e", "f"];

print Dumper(\%h);

Another issue

At the end of your question, you are telling us

Yes, I could do it without an array variable.
$h {A} = ("a", "b", "c");
But is there another way too? I want to have @l and no blocks.

But I don't think this line does what you expect, print Dumper ($h{A}); would show this:

'c'

You say that you didn't want blocks, I read this as memory fragmentation, but Perl doesn't let you control how memory is being used. On the contrary!

Randomization

Perl uses techniques to discourage side-channel attacks that rely on certain expectations about memory layout. Read keys - Perldoc Browser and Algorithmic Complexity Attacks for more. To give you an idea, take a look at this example, which is sure to shock a C programmer (which I think you are):

#!/usr/bin/perl

use strict;
use warnings;
use 5.010; 

for (1..10) {
    my %h = ( 
        "A" => ["a", "b", "c"],
        "B" => ["d", "e", "f"]
    );
    say keys %h;
}

Its output is like follows

AB
BA
BA
BA
AB
AB
AB
AB
BA
AB

... sometimes, but most of the time its different, also from start to start. Try it yourself!

2
  • Nothing is wrong with it. But it is just a sample I use for demonstration. My real code is not able to do that kind of initialization.
    – chris01
    Feb 6, 2021 at 8:41
  • 3
    In that case you should alter your code sample. The sample should illustrate the kind of problem you actually have not something related. So if you don't use literals, please show us variables. If you do things 1000s of times, tell us in the question. Your question has a lot of issues that's why I voted to close and voted down, please improve it to show your actual needs! Thanks in advance.
    – Wolf
    Feb 6, 2021 at 8:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.