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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble building array 3

Status
Not open for further replies.

calabama

Programmer
Feb 19, 2001
180
US
There are 2 loops, the first one iterates thru a list of names [red]@names[/red] and starts to build the first line of code while identifying a unique variable $UNIQUE_PD_ID$x as well as the name of the pull-down menu SELECT NAME ="M$x".

The second loop looks for a match of a stored pull-down selection $STORED_NAME , and finishes build the pull-down menu.

I want to end up with an array IE:@pulldown_menu_names=("$UNIQUE_PD_ID1","$UNIQUE_PD_ID2","$UNIQUE_PD_ID3","$UNIQUE_PD_ID4","$UNIQUE_PD_ID5","$UNIQUE_PD_ID6");

Thanks for your help

Cal

------------------- Start code --------------
[red]@names[/red] =("Gina","Lou","Shari","Louis","Lee","June");

$STORED_NAME = "June";

for ($x=1; $x <= [red]$names[/red] ; $x++)
{
my $UNIQUE_PD_ID$x = qq|<SELECT NAME =&quot;M$x&quot;>|;


for ($x=1; $x <= [red]$names[/red] ; $x++)
{if ([red]$names[/red] [$x] =~ /$STORED_NAME/){
$UNIQUE_PD_ID$x .= &quot;<OPTION SELECTED>[red]$names[/red] [$x]</OPTION>&quot;;
next;}
$UNIQUE_PD_ID$x .= &quot;<OPTION>[red]$names[/red] [$x]</OPTION>&quot;;}
$UNIQUE_PD_ID$x .= &quot;</SELECT>&quot;;
}

Thanks,

Cal In the begining
Let us first assume that there was nothing to begin with.
 
Also you should check out toolkits previous post about interpolation in qw()

just use:
my @pd_select = ($NEW_BUS_PD0,$NEW_BUS_PD1,$NEW_BUS_PD2);
 
Toolkit ++ and Yauncin ++, :cool:

I really appreciate you guys and everyone else in this forum that contribute immensely to the enlightenment of lesser knowledgeable people like myself.

I wish that I could be as concise as you when communicating in forums.

We are all striving to become better monks, communicators and mentors it's just that better is a relative term.


The last question that I posed was never address though.
So I'll try again.

If the array is created this way then what is the best way to pass each element of @pd_select to the loop. Should it be $pd_select[$x] or something else.



my @pd_select = ($NEW_BUS_PD0,$NEW_BUS_PD1,$NEW_BUS_PD2,$NEW_BUS_PD3);
-------------------------------------


$&quot; = &quot;\n&quot;;

my @names = @names;


my $i = 0;
@pd = map { $select = $_; qq{

<select name=&quot;M@{[$i++]}&quot;>
@{[ map { /$select/ ?
qq{<option selected>$_</option>} :
qq{<option>$_</option>} } @names ]}
</select>

} } @names;

foreach( @pd ) {
print;
}

-----------------
Thanks,

Cal
In the begining
Let us first assume that there was nothing to begin with.
 
calabama
You shouldn't need to do anything like that. The code actually loops through the @names array twice:
[tt]
set separator between array elements when interpolating to newline
$&quot; = &quot;\n&quot;;

aren't @names and @pd_select identical?
my @names = qw(Gina Lou Shari Louis Lee June);

my $i = 0;

start of outer loop
@pd = map {

store current element in @name to $select
$select = $_;

qq{
start select element using sneaky @{[...]} operation to print $i, then increment
<select name=&quot;M@{[$i++]}&quot;>

first option in select
<option selected>$select</option>

start of inner loop
@{[ map {

show normal option, after filtering out current selected option with grep
qq{<option>$_</option>} } grep { $_ ne $select }

@names ]}
end of inner loop


</select>

} } @names;
end of outer loop

foreach( @pd ) {
print;
}
[/tt]
Cheers, Neil :)
 
Hello Toolkit,

You asked [red]aren't @names and @pd_select identical?[/red]

@names is stored in an array located in stored_names.pm. These names should rarely change.

@names =(&quot;
&quot;,&quot;Gina Amelino-Flack&quot;,&quot;
Lou Camero&quot;,&quot;
Shari Corona&quot;,&quot;
Louis Fiandeiro&quot;,&quot;
Lee Good&quot;,&quot;
Bill Kelch&quot;,&quot;
Fr.Ben Manding&quot;,&quot;
Lumen Mendoza&quot;,&quot;
June Moroney&quot;,&quot;
Anita Tejeda&quot;,&quot;
Diane Tyler&quot;,&quot;
Fran Zuvela&quot;,&quot;is substituting for:&quot;);


@pd_select is stored in individual variables located in saved.pm.These elements change much more frequently. They include GUEST and substitute names.

my @pd_select = ($NEW_BUS_PD0,$NEW_BUS_PD1,$NEW_BUS_PD2,$NEW_BUS_PD3);

$GUEST_NAMES = &quot;Tim Briggs&quot;;
$SUB_NAME1 = &quot;Joe Bob&quot;;
$MEM_NAME1 = &quot;Diane Tyler&quot;;
$SUB_NAME2 = &quot;&quot;;
$MEM_NAME2 = &quot;is substituting for:&quot;;
$SUB_NAME3 = &quot;&quot;;
$MEM_NAME3 = &quot;is substituting for:&quot;;
$SUB_NAME4 = &quot;&quot;;
$MEM_NAME4 = &quot;is substituting for:&quot;;
$NEW_BUS_PD1 = &quot;Substitute 1&quot;;
$NEW_BUS_TXT1 = &quot;abc&quot;;
$NEW_BUS_PD2 = &quot;June Moroney&quot;;
$NEW_BUS_TXT2 = &quot;def&quot;;
$NEW_BUS_PD3 = &quot;GUEST&quot;;
$NEW_BUS_TXT3 = &quot;ghi&quot;;

In the begining
Let us first assume that there was nothing to begin with.
 
Right, so from what I understand, you want an option for each string in '@names', with a selected specified by the strings in '@pd_select'. The following code will do this. Note I have replaced '@pd' with '%pd' keyed by the entry in @pd_select:
Code:
$&quot; = &quot;\n&quot;;

my @pd_selects = qw(Gina Lee);
my @names      = qw(Gina Lou Shari Louis Lee June);

my $i = 0;
%pd = map { ( $select = $_ ) => qq{

<select name=&quot;M@{[$i++]}&quot;>
<option selected>$select</option>
@{[ map { qq{<option>$_</option>} } grep { $_ ne $select } @names ]}
</select>

} } @pd_selects;

foreach( sort keys %pd ) {
    print &quot;$_: $pd{$_}\n&quot;;
}
This produces output like:
Code:
Gina: 

<select name=&quot;M0&quot;>
<option selected>Gina</option>
<option>Lou</option>
<option>Shari</option>
<option>Louis</option>
<option>Lee</option>
<option>June</option>
</select>


Lee: 

<select name=&quot;M1&quot;>
<option selected>Lee</option>
<option>Gina</option>
<option>Lou</option>
<option>Shari</option>
<option>Louis</option>
<option>June</option>
</select>
Hope this gives you enough to go on. Cheers, Neil
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top