Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array Question

Status
Not open for further replies.

wood

MIS
Aug 3, 2000
139
CA
I am using an array to set values for a select lists in HTML. I have 10 select lists that are being passed the array called "@subtotaloptions_avail" or "@subtotaloptions_sel" with a 1-10 at the end of the array.

I am looping through available options and adding them to the array, however, I want to change the name of the array within the loop.

Here is an example of what I want to do, but it is giving me errors.

Could you please help by telling me how to change the name of the array within the loop?

Thanks!

Code:
my $z=2;
for ($z <= 10){
# Set up the selected options for the custom fields.
    foreach my $selection (@selections2) {
	if ($listv[$i] eq $selection) {
    	    $found = 1;
	}	
    }
    if ($found ne 1) {
	push(@subtotaloptions_avail$z, "$listk[$i]:$listv[$i]" );
    } else {
	push(@subtotaloptions_sel$z, "$listk[$i]:$listv[$i]" );
    }
    $found = 0;
    $z++
}

I know that the problem is with these lines of code:
@subtotaloptions_avail$z
and
@subtotaloptions_sel$z

what I want is:
@subtotaloptions_sel2
@subtotaloptions_sel3
@subtotaloptions_sel4
etc.

 
What you're looking for is a soft (or "symbolic") reference. They're a horrible idea: an idea that I've manage to pulverise into most forum-regulars' heads by now :) Have a read of this to see an explanation of why they're not a very good idea.

For this, you're far, far, far, far (you get the picture) better using a 2-d array:
Code:
push( @{ $subtotaloptions_sel[ $z ] }, "$listk[$i]:$listv[$i]" );
 
Actually the code you gave me does not work. It puts a value of ARRAY(0x90559f38) in one of the list boxes.

How would I use a 2-d array here?
 
Use a hash instead of trying to rename arrays:

Code:
my $z=2;
for ($z <= 10){
# Set up the selected options for the custom fields.
    foreach my $selection (@selections2) {
    if ($listv[$i] eq $selection) {
            $found = 1;
    }    
    }
    if ($found ne 1) {
    [b]$subtotaloptions_avail{$z}[/b] = "$listk[$i]:$listv[$i]" );
    } else {
    [b]$subtotaloptions_sel{$z}[/b] = "$listk[$i]:$listv[$i]" );
    }
    $found = 0;
    $z++
}

The original order of the data might be lost using a hash.

Or use a 2D array like ishnid suggests. The 2D array can maintain order and give you the desired output.
 
Actually the code you gave me does not work. It puts a value of ARRAY(0x90559f38) in one of the list boxes.
That's the reference to the second dimension of your array. Google for "perllol" - that'll explain multidimensional data structures far better than I could.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top