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
7 16814
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
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.
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
You have to use eval with scalar
for ex.
my $len = eval ( "scalar \@array${i}");
Regards,
Manimaran.K
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];
-
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.
Nice Feedback Kevin...Thanks
Regards,
Manimaran
Sign in to post your reply or Sign up for a free account.
Similar topics
by: cpp |
last post by:
I am trying to find out how to assign the name of a variable (aka
identifier) given a string for this name. For example:
string s = "whatever";
How can I obtain the following expression:...
|
by: paladin.rithe |
last post by:
I have no clue if this can be done, but I thought I'd ask anyway. I
have a modular system where I store module information in a database.
One of the types of modules that I have is one that,...
|
by: Angelos |
last post by:
Hello,
I need to dynamically specify the name of a variable. I just read that
$varA = "Cat";
echo $$varA;
OUTPUT: Cat
What I try to establish is somehow add a bit of text on my dynamic...
|
by: Mark S. |
last post by:
As I understand it, C# doesn't offer dynamic variable names. Below is my
attempted workaround. Is what I'm doing possible?
FYI, I already read all the "why in the world do you need dynamic...
|
by: lrlebron |
last post by:
I am trying to create popup windows with dynamic variable names and
then see if they exist or not.
For example, if I call the function like so openWin("2600","2000") I
would like the function to...
|
by: X l e c t r i c |
last post by:
Here:
http://bigbangfodder.fileave.com/res/sandr.html
I'm trying to use string.replace() for a basic search and replace form
using textarea values as the regexp and replacement values for...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |