views:

103

answers:

2

Hi,

I would like to create a list of records. I can add one record just fine:

my $records = [
  { ID => 5342755,
    NAME => 'Me',
  } ,
 ];

When I try to add another record, the other records disappear:

$records = [  { 
  ID => 1212121, 
 } ];

What is the problem and how can I resolve this?

+7  A: 

The problem is you are overwritting the value of $record so that there is only ever one value in the array. Perhaps try the following instead:

my $records = [
  { ID => 5342755,
    NAME => 'Me',
  } ,
 ];

push @$records, { 
  ID => 1212121, 
 };
Matthew Scharley
Thank you very much .
A: 

You override your variable...

When you declare:

my $records = [date structure here];

you really declare an array ref, if you are a newbie then try (more intuitive)

my @records = (  
               { 
                 ID => 54321,
                 NAME => 'bar',
                } ,
);

push @records, {ID => 12345, NAME => 'foo'};
print $records[1]->{NAME};

That would print 'foo'

maozet