Initialize an Array of Pointer to an array in PERL

Initialize an Array of Pointers to an array in PERL

Page 1 of 1

1 Replies - 1221 Views - Last Post: 27 January 2010 - 06:50 AM

#1 sohailabbas4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 26-January 10

Initialize an Array of Pointer to an array in PERL

Post icon  Posted 26 January 2010 - 03:56 PM

$cellDB[0] =	{
	  'cellLeakage' : '15.1484',
	  'cellArea':'234'
		   }

$cellDB[1] =	{
	  'cellLeakage' : '16.666',
	  'cellArea':'323'
		   }


No I created an array of Pointer
$lib[0]=\@cellDB; where $lib[0] points to cellDB arrays.
Read more cellDB and put the pointer again in new $lib[1];
$lib[1]=\@cellDB where This point will point to new cell of cellDBs

But unfortunately when I read the firts cell set in the $lib[0] and then read in the new cell set in $lib[1] some how $lib[0] is always written over. No clue why is this happening.

I think it is overwriting the pointer in the $lib[0]. Please advise.

Is This A Good Question/Topic? 0
  • +

Replies To: Initialize an Array of Pointer to an array in PERL

#2 dsherohman  Icon User is offline

  • Perl Parson
  • member icon

Reputation: 226
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: Initialize an Array of Pointer to an array in PERL

Posted 27 January 2010 - 06:50 AM

View Postsohailabbas4, on 26 Jan, 2010 - 10:56 PM, said:

No I created an array of Pointer
$lib[0]=\@cellDB; where $lib[0] points to cellDB arrays.
Read more cellDB and put the pointer again in new $lib[1];
$lib[1]=\@cellDB where This point will point to new cell of cellDBs

But unfortunately when I read the firts cell set in the $lib[0] and then read in the new cell set in $lib[1] some how $lib[0] is always written over. No clue why is this happening.

I think it is overwriting the pointer in the $lib[0]. Please advise.

The references are just fine, the issue is that you're changing the referenced array. If you want $lib[0] and $lib[1] to produce different data when dereferenced, you have to point them at two different arrays instead of the same one.

If you need more help in figuring out just how to do that, please post a small sample of your code which is giving you trouble (i.e., code which builds @cellDB, sets $lib[0], modifies @cellDB, and sets $lib[1]) so that I can see how you're doing it and I can show you the easiest way to modify the code you post so that it will work correctly.


(Side note: Technically, there's no such thing as a pointer in Perl; Perl has references instead. For most practical purposes, though, they behave the same.)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1