473,385 Members | 1,472 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 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
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,509 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

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

Similar topics

10
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:...
2
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,...
5
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...
3
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...
2
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...
2
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...
0
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...
0
isladogs
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...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.