QED
QED

Reputation: 9913

Perl array dumping

I do this:

my @items = [];

sub pushItem {
   my $itemId = "i" . sprintf("%d", $_[0]);
   push(@items, $itemId);
}

sub popItems {
   $itemsXml = "<key>items</key>\n<array>\n";

   foreach (@items) {
      $itemsXml .= "<string>$_</string>\n";
   }

   $itemsXml .= "</array>\n";

   return $itemsXml;
}

pushItem(0);
pushItem(1);
pushItem(2);
print popItems();

I get this:

 <key>items</key>
 <array>
 <string>ARRAY(0x7fa1730070d0)</string>
 <string>i0</string>
 <string>i1</string>
 <string>i2</string>
 </array>

The problem of course being:

<string>ARRAY(0x7fa1730070d0)</string>

Upvotes: 0

Views: 68

Answers (3)

ikegami
ikegami

Reputation: 385590

[] creates an array and a reference to that array, and returns the latter. This means the following populates @items with a reference to a second array in addition to creating @items:

 my @items = [];

You simply want

 my @items;

If you insist on assigning something to it, you'd want to assign a list of zero scalars to it, and you can do that as follows:

 my @items = ();

But really, that's just a waste (since you're clearing an empty array) and it adds needless complexity (code that does nothing).


Just to be clear, @items = (); (as opposed to my @items = ();) is still useful, though only rarely. I think the only time I've used it is in code of the following form:

my @buf;
while (my $item = get()) {
   if (check($item)) {
      something(@buf);
      @buf = ();
   }

   push @buf, $item;
}

something(@buf) if @buf;

Upvotes: 3

theta-fish
theta-fish

Reputation: 308

Your problem is your first line:

my @items = [];

It should be:

my @items = ();

The problem is that you have assigned an array reference to the first element of the array @items, and then later you push a bunch of other values onto it.

If you initialize it using the parens instead, it does what you want, which is create an empty array. This becomes more apparent if you comment out your calls to pushItem and run the script.

Upvotes: 2

Miguel Prz
Miguel Prz

Reputation: 13792

my @items = [];

should be

my @items = ();

Upvotes: 5

Related Questions