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.
 
The following extremely obfuscated ;-) code performs the same functionality without having to resort to symbolic variables:
Code:
my @names = qw(Gina Lou Shari Louis Lee June);

$selected = qq(June);

my $i = 0;
my @pullDowns = map { qq(<select name=&quot;M@{[$i++]}&quot;>$_</select>\n) }
                map { /$selected/ ?
                      qq(<option selected>$_</option>) :
                      qq(<option>$_</option>)
                    } @names;

foreach( @pullDowns ) {
    print;
}
Which produces:
Code:
<select name=&quot;M0&quot;><option>Gina</option></select>
<select name=&quot;M1&quot;><option>Lou</option></select>
<select name=&quot;M2&quot;><option>Shari</option></select>
<select name=&quot;M3&quot;><option>Louis</option></select>
<select name=&quot;M4&quot;><option>Lee</option></select>
<select name=&quot;M5&quot;><option selected>June</option></select>
Is this what you really want though?
Cheers, Neil :)
 
Toolkit ,

How does the &quot;my&quot; at the begining and the &quot;qw&quot; change the arrray?

Cal In the begining
Let us first assume that there was nothing to begin with.
 
my applies lexical scoping to the variable. This limits the scope within which the variable exists. When not enclosed within a block, the variable is scoped to the package level, so if you have the following code:
Code:
file XYZ.pm:
package XYZ;
my $a;

file ABC.pl:
use XYZ;
$XYZ::a = 3;  # accesses variable in  XYZ package
qw is a minimal punctuation method of assigning an array:
Code:
my @arr = qw( a b c d );
IS IDENTICAL TO:
my @arr = ( 'a', 'b', 'c', 'd' );
More information on these commands can be found in the perlfunc manual pages.
Cheers, Neil :)
 
Hello Toolkit,

1.) If I already have a stored variable
[red]@names=(&quot;Gina&quot;,&quot;Lou &quot;,&quot;Shari&quot;,&quot;Louis&quot;,&quot;Lee&quot;,&quot;June&quot;);[/red]
how do I assign &quot;my&quot; to the begining of it or do I need to like the following example?

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

2.) [red]$selected = qq(June);[/red] is also suppose to be a stored variable

The second loop looks for a match of a stored pull-down selection ,
$STORED_NAME1 =&quot;whoever&quot; ,$STORED_NAME2 =&quot;whoever&quot; ,
$STORED_NAME3 =&quot;whoever&quot; ,$STORED_NAME4 =&quot;whoever&quot; ETC.

Thanks In the begining
Let us first assume that there was nothing to begin with.
 

How do I assign &quot;my&quot; to the begining of it or do I need to like the following example?

Would the example below be an acceptable way to accomplish this?


my @names = @names;

Thanks

In the begining
Let us first assume that there was nothing to begin with.
 
Don't get hung up on my. If you already have a @names array, you can use this :)
 
Calabama,

This is something you might be interested in:

local @names;

The local function is often confused with the my function.

my scopes a 'new' variable.
local lets you molest the affected variable within the current block, and the ORIGINAL value (before the 'local') is restored to the variable.

So if you want to screw with the global @names array in the current block (and ALL functions called from that block) then you could use local.

Happy scriptin buddy!

--Jim
 
OOOPS. I never finished my sentence.

To complete:
local lets you molest the affected variable within the current block, and the ORIGINAL value (before the 'local') is restored to the variable (and now the rest) when Perl exits that block.

Sorry bout that. Clarity is a good thing.

--Jim
 
Does anyone know can an [red]&quot;if&quot; statement[/red] exist in a map function? If so how would you go about it?

Thanks,

Cal In the begining
Let us first assume that there was nothing to begin with.
 
The first argument in a map function can be a block of code, so you could do all kinds of different things. For your question, normally the ternary operator ?: will do:
Code:
@f_words = map { (/^f/) ? $_ : 'undef' } @list_of_words;
So there are some @f_words. ;-)

Or you could just say:
Code:
@some_array = map { if($_ eq &quot;test&quot;){ 
 dothis()} else { dothat() } } @list_for_testing;

Hope this helps.

--Jim
 
You can also use the grep function if you just want to filter an array:
Code:
@f_words = grep { /^f/ } @list_of_words;
Cheers, Neil :)
 


TOOLKIT

The extremely obfuscated code that you had posted is very close to what I needed. It is much cleaner.

The difference is that each pull down menu should have all the names in it. Not sure how to do this.


Which produces:

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


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


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


instead of

<select name=&quot;M1&quot;> <option>Lou</option></select>
<select name=&quot;M2&quot;> <option>Shari</option></select>
<select name=&quot;M3&quot;> <option selected>June</option></select>
In the begining
Let us first assume that there was nothing to begin with.
 
I think the following even more obfuscated code should do the trick:
Code:
$&quot; = &quot;\n&quot;;
 
my @names = qw(Gina Lou Shari Louis Lee June);
 
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;
}
Cheers, Neil s-)
 
Using the $select variable every pull down menu has the same item selected.

If $select = &quot;Bill Kelch&quot; there is no match the selected is defaulting to the first in the list. I probabley should pull the $selected from an array.

my @pd_select = qw($NEW_BUS_PD0 $NEW_BUS_PD1 $NEW_BUS_PD2);


---------------------------------------

my $i = 0;
$select = &quot;Bill Kelch&quot;;

@pd_menus = map { $select = $_; qq{

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

} } @names;

In the begining
Let us first assume that there was nothing to begin with.
 
The latest code I posted should produce:
[tt]
<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>

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

<select name=&quot;M2&quot;>
<option>Gina</option>
<option>Lou</option>
<option selected>Shari</option>
<option>Louis</option>
<option>Lee</option>
<option>June</option>
</select>
etc...
[/tt]
If you want the selected option to always appear at the top of the select list, then the following code should do the trick:
Code:
$&quot; = &quot;\n&quot;;

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>

} } @names;

foreach( @pd ) {
    print;
}
Which will produce output like:
[tt]
<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>

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

<select name=&quot;M2&quot;>
<option selected>Shari</option>
<option>Gina</option>
<option>Lou</option>
<option>Louis</option>
<option>Lee</option>
<option>June</option>
</select>
etc...
[/tt]
Cheers, Neil :cool:
 
PS: Calabama. You cannot use [tt]qw[/tt] when interpolating variables, or when your variables contain spaces. From the [tt]perlop[/tt] manual pages:
Code:
        Customary  Generic     Meaning    Interpolates
            ''       q{}       Literal         no
            &quot;&quot;      qq{}       Literal         yes
            ``      qx{}       Command         yes
                    qw{}      Word list        no
            //       m{}    Pattern match      yes
                     s{}{}   Substitution      yes
                    tr{}{}   Translation       no
For that, just use the original:
Code:
@arr = ( $xyz, &quot;contains spaces&quot;, etc... );
Cheers, Neil ;-)
 

Toolkit,

Thanks so much for all your help
So if the array just has commas in between variables that should work.
I don't need any single or double quote for a list unless the elements consist of literal strings.

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

if the array is created this way then what is the best way to pass each variable to the loop. Should it be [red]$pd_select[$x][/red] 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 = [red]$pd_select[$i][/red]; qq{

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

} } @names;

foreach( @pd ) {
print;
}




When I look for the map fuction I can't seem find much information about it. In the begining
Let us first assume that there was nothing to begin with.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top