using dynamic variable name in PERL... | Member | | Join Date: Dec 2007
Posts: 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
| | Newbie | | Join Date: Jul 2007 Location: chennai
Posts: 22
| | | 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 -
for (my $i = 0;$i < 5; $i++) {
-
eval ( "push \@array${i},\"\$i\"" );
-
print "\@array${i} @array${i} \n";
-
}
-
regards,
Manimaran.k
| | Member | | Join Date: Dec 2007
Posts: 40
| | | 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.. -
for($i=0; $i<=$#filedata; $i++)
-
{
-
@linedata = split (/\, / , $total[$i]);
-
foreach $dt (@data)
-
{
-
eval ( "push \@array${i},\"\$dt\"" );
-
$len = scalar @array${i};
-
}
-
}
-
Pl provide help for this also.
|  | Site Moderator | | Join Date: May 2007 Location: New Hampshire
Posts: 2,565
| | | 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.. -
for($i=0; $i<=$#filedata; $i++)
-
{
-
@linedata = split (/\, / , $total[$i]);
-
foreach $dt (@data)
-
{
-
eval ( "push \@array${i},\"\$dt\"" );
-
$len = scalar @array${i};
-
}
-
}
-
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
| | | 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
|  | Expert | | Join Date: Jan 2007 Location: Southern California USA
Posts: 4,091
| | | 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: -
my $input = 5;
-
my $HofA = ();
-
for (1..$input) {
-
$HofA{$_} = [];
-
}
-
# to add data to the arrays:
-
push @{ $HofA{3} } , 'foo';
-
-
# to print data
-
print $HofA{3}[0];
-
|  | Expert | | Join Date: Jan 2007 Location: Southern California USA
Posts: 4,091
| | | re: using dynamic variable name in PERL... Quote:
Originally Posted by manimarank HI
you can use eval function like -
for (my $i = 0;$i < 5; $i++) {
-
eval ( "push \@array${i},\"\$i\"" );
-
print "\@array${i} @array${i} \n";
-
}
-
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
| | | re: using dynamic variable name in PERL...
Nice Feedback Kevin...Thanks
Regards,
Manimaran
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,223 network members.
|