469,281 Members | 2,450 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,281 developers. It's quick & easy.

using dynamic variable name in PERL...

40
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
Dec 20 '07 #1
7 16022
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
Dec 20 '07 #2
cnsabar
40
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.
Dec 20 '07 #3
numberwhun
3,503 Expert Mod 2GB
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
Dec 20 '07 #4
You have to use eval with scalar

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

Regards,
Manimaran.K
Dec 21 '07 #5
KevinADC
4,059 Expert 2GB
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.  
Dec 21 '07 #6
KevinADC
4,059 Expert 2GB
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.
Dec 21 '07 #7
Nice Feedback Kevin...Thanks

Regards,
Manimaran
Dec 21 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

10 posts views Thread by cpp | last post: by
2 posts views Thread by paladin.rithe | last post: by
5 posts views Thread by Angelos | last post: by
2 posts views Thread by X l e c t r i c | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.