?
Solved

lists in perl, can i put a variable in the list definition somehow?

Posted on 2003-03-30
8
Medium Priority
?
175 Views
Last Modified: 2010-03-05
this is what i want to do, is there a way to get this to work? if not, what is the workaround?

$selector = "1";
@myList$selector = ("test1","test2");

print "@myList1";
0
Comment
Question by:inudaci
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 48

Expert Comment

by:Tintin
ID: 8235563
It's not entirely clear what you are trying to achieve.  Perhaps you could give a more detailed description of what you are trying to do.

If you simply want an element from an array, just do something like:

@myList = qw(test1 test2);
print "First array element is $myList[0]\n";



0
 
LVL 5

Expert Comment

by:burtdav
ID: 8235574
# http://www.perldoc.com/perl5.6/pod/func/splice.html

$selector = "0";
$list_to_add = ("test1","test2");

# version inserting at given offset
splice @myList, $selector, 0 = $list_to_add;

# version replacing existing elements
# splice @myList, $selector, scalar($list_to_add) = $list_to_add;

print "@myList1";
0
 

Author Comment

by:inudaci
ID: 8235593
thanks for your response, i'll try and clearify, hold on
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:inudaci
ID: 8235632
burtdav,

im sure this works, what i need though is more like this, can i do this? a list within a list...

$selector = "0";
@list_to_add = ("test1","test2");

# version inserting at given offset
splice @myList, $selector, 0 = @list_to_add;

# version replacing existing elements
# splice @myList, $selector, scalar(@list_to_add) = @list_to_add;

#ex.
print "$myList1[$list_to_add[0]]";

this surely doesnt work but do you understand what i want?
0
 

Author Comment

by:inudaci
ID: 8235687
and i couldnt get the code that you posted to work no
0
 
LVL 20

Expert Comment

by:jmcg
ID: 8236181
I'm also very unclear on what it is you want to do.

You can write a perl list with sublists, but it all evaluates to a single linear array.

@alist = ( 'test1', 'test2', ( 'test3', 'test4', ()), 'test6');

but this turns out not to be different from

@alist = qw(test1 test2 test3 test4 test6);

To get more complicated data structures, you have to start using perl "references" [see "perldoc perlref" if you have a command line].

@blist = ( 'test1', 'test2', ['test3', 'test4', []], 'test6');

print $blist[0]; # prints test1
print $blist[2]->[1]; # prints test4

Naturally, you can play games with assignment and splice and other operators to insert refs into an array, too.
0
 
LVL 7

Accepted Solution

by:
rugdog earned 80 total points
ID: 8239777
if you have several defined @myList (e.g. @myList1, @myList2, etc.) you could use eval:

$selector = "1";
$cmd="\@myList$selector = (\"test1\",\"test2\");";
eval $cmd;

print "@myList1";

this will work.


However, a better way of doing multi-dimensional arrays in perl is by using array references:

@arr=(["test0.0,"test0.1"],["test1.0","test1.1"]);

for($i=0;$i<=$#arr;$i++){
   for($j=0;$j<=$#{$arr[$i]};$j++){
      print $arr[$i]->[$j]."\n";
    }
}
0
 

Author Comment

by:inudaci
ID: 8242985
thanks for good answers, you helped me
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question