Avatar of Dan-LL
Dan-LL asked on

loop through string array

Can someone tell me if this can be done and how as I have almost got it right, erm no I have not...

ozo gave me this one liner that works a treat
$r1t = (undef, $onename, $twoname,  $threename, $fourname, $fivename)[$r1t] || $r1t;

for my $i (1 .. 4){
$r.$i.t = (undef, $onename, $twoname,  $threename, $fourname, $fivename)[$r.$i.t] || $r.$i.t;
}

it does not return the same values as writing it long hand like this, any ideas if the code can be looped.

$r1t = (undef, $onename, $twoname,  $threename, $fourname, $fivename)[$r1t] || $r1t;
$r2 = (undef, $onename, $twoname,  $threename, $fourname, $fivename)[$r2t|| $r2;
$r3 = (undef, $onename, $twoname,  $threename, $fourname, $fivename)[$r3 || $r3;
$r4 = (undef, $onename, $twoname,  $threename, $fourname, $fivename)[$r4] || $r4t
Scripting LanguagesPerl

Avatar of undefined
Last Comment
Dan-LL

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
ozo

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
Dan-LL

corrected the $ 's below

it does not return the same values as writing it long hand like this, any ideas if the code can be looped.

$r1t = (undef, $onename, $twoname,  $threename, $fourname, $fivename)[$r1t] || $r1t;
$r2t = (undef, $onename, $twoname,  $threename, $fourname, $fivename)[$r2t|| $r2t;
$r3t = (undef, $onename, $twoname,  $threename, $fourname, $fivename)[$r3t || $r3t;
$r4t = (undef, $onename, $twoname,  $threename, $fourname, $fivename)[$r4t] || $r4t
ASKER
Dan-LL

testing it, don;t worried about the missing t's that was my fault....
ASKER
Dan-LL

hey thanks ozo, I made that look harder than it was, easy when you know how thanks,

can I just as (you don't have to answer this if it takes a lot of work), how can I do this then...

...
for my $n (1 .. 4){
my $r.$n.t = $GET->param('r.$n.t');  
}

you get the idea, GET the values from the form and set the values ready for the answer you just did for me...
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Adam314

There might be an easier way to accomplish what you are trying.  If you post more about what you are trying to do, or more of your script, we might be able to make some suggestions.
ozo

$_ = $GET->param($_) for ($r1t ,$r2 ,$r3,$r4)
ASKER
Dan-LL

ozo, that does not appear to work,

Adam, sorry not cutting you out but ozo answered the question and I have been cheeky and asked another question in the same thread, did not expect more help, lol...

don't want to upset anyone, ozo and adam write more software for me than I write myself these days....
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
Dan-LL

$_ = $GET->param($_) for ($r1t ,$r2t ,$r3t,$r4t);

i read that as geting a $ in the param and setting the $ as the $ i.e

$r1t = $GET->param('$r1t');

the value in the param() is not a $ type, is this why it does not work

I have

$r1t = $GET->param('r1t');
SOLUTION
ozo

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
Dan-LL

Thanks, I split the point althoguh you got all 125 just so anyone reading this will see the 2 working solutions you provide, many thanks