• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 179
  • Last Modified:

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

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
inudaci
Asked:
inudaci
1 Solution
 
TintinCommented:
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
 
burtdavCommented:
# 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
 
inudaciAuthor Commented:
thanks for your response, i'll try and clearify, hold on
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
inudaciAuthor Commented:
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
 
inudaciAuthor Commented:
and i couldnt get the code that you posted to work no
0
 
jmcgOwnerCommented:
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
 
rugdogCommented:
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
 
inudaciAuthor Commented:
thanks for good answers, you helped me
0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

Tackle projects and never again get stuck behind a technical roadblock.
Join Now