Connecting Tech Pros Worldwide Help | Site Map

using dynamic variable name in PERL...

Member
 
Join Date: Dec 2007
Posts: 40
#1: Dec 20 '07
Hi ,

I need to create a dynamic variable name in array using Perl...
For example.. if i give the input no as 5..

The program has to create a variable name as
@array1
@array2
@array3
@array4
@array5

kindly help anyone regarding this.

Thanks,
Sabarish
Newbie
 
Join Date: Jul 2007
Location: chennai
Posts: 22
#2: Dec 20 '07

re: using dynamic variable name in PERL...


Quote:

Originally Posted by cnsabar

Hi ,

I need to create a dynamic variable name in array using Perl...
For example.. if i give the input no as 5..

The program has to create a variable name as
@array1
@array2
@array3
@array4
@array5

kindly help anyone regarding this.

Thanks,
Sabarish

HI
you can use eval function like
Expand|Select|Wrap|Line Numbers
  1. for (my $i = 0;$i < 5; $i++) {
  2.   eval ( "push \@array${i},\"\$i\"" );
  3.   print "\@array${i} @array${i} \n";
  4. }
  5.  
regards,
Manimaran.k
Member
 
Join Date: Dec 2007
Posts: 40
#3: Dec 20 '07

re: using dynamic variable name in PERL...


Hi, Manimaran.,

thanks for your suggestion... I am in getting error when I accesed the array length using scalar keyword..

Expand|Select|Wrap|Line Numbers
  1. for($i=0; $i<=$#filedata; $i++)
  2. {
  3. @linedata = split (/\, / , $total[$i]);
  4. foreach $dt (@data)
  5. {
  6.     eval ( "push \@array${i},\"\$dt\"" );
  7.         $len = scalar @array${i};
  8. }
  9. }
  10.  
Pl provide help for this also.
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,565
#4: Dec 20 '07

re: using dynamic variable name in PERL...


Quote:

Originally Posted by cnsabar

Hi, Manimaran.,

thanks for your suggestion... I am in getting error when I accesed the array length using scalar keyword..

Expand|Select|Wrap|Line Numbers
  1. for($i=0; $i<=$#filedata; $i++)
  2. {
  3. @linedata = split (/\, / , $total[$i]);
  4. foreach $dt (@data)
  5. {
  6.     eval ( "push \@array${i},\"\$dt\"" );
  7.         $len = scalar @array${i};
  8. }
  9. }
  10.  
Pl provide help for this also.

Its great to tell us that you are getting an error when running the code, but can you please post the error here for us to see?

Regards,

Jeff
Newbie
 
Join Date: Jul 2007
Location: chennai
Posts: 22
#5: Dec 21 '07

re: using dynamic variable name in PERL...


You have to use eval with scalar

for ex.
my $len = eval ( "scalar \@array${i}");

Regards,
Manimaran.K
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#6: Dec 21 '07

re: using dynamic variable name in PERL...


Quote:

Originally Posted by cnsabar

Hi ,

I need to create a dynamic variable name in array using Perl...
For example.. if i give the input no as 5..

The program has to create a variable name as
@array1
@array2
@array3
@array4
@array5

kindly help anyone regarding this.

Thanks,
Sabarish

This type of scenario is what hashes are for. Creating dynamic variable names is very bad in real perl programs and should be avoided at all cost. But hash keys can be created dynaminally and the value of the keys can be arrays:

Expand|Select|Wrap|Line Numbers
  1. my $input = 5;
  2. my $HofA = ();
  3. for (1..$input) {
  4.    $HofA{$_} = [];
  5. }
  6. # to add data to the arrays:
  7. push @{ $HofA{3} } , 'foo';
  8.  
  9. # to print data
  10. print $HofA{3}[0];
  11.  
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#7: Dec 21 '07

re: using dynamic variable name in PERL...


Quote:

Originally Posted by manimarank

HI
you can use eval function like

Expand|Select|Wrap|Line Numbers
  1. for (my $i = 0;$i < 5; $i++) {
  2.   eval ( "push \@array${i},\"\$i\"" );
  3.   print "\@array${i} @array${i} \n";
  4. }
  5.  
regards,
Manimaran.k

Learn to use references and complex data structures. Anytime you see a person say they want to create dynamic variable names with perl they should be told to use a hash.
Newbie
 
Join Date: Jul 2007
Location: chennai
Posts: 22
#8: Dec 21 '07

re: using dynamic variable name in PERL...


Nice Feedback Kevin...Thanks

Regards,
Manimaran
Reply