Gossamer Forum
Home : General : Perl Programming :

incrementing a variable

Quote Reply
incrementing a variable
my form has dynamically generated input for file-to-upload-$u using syntax below, which works for the input form.

i want to do something like this:

Code:

my ($found, $num_files, $u);
for ($u=1;$u<=$MAXIMUM_FILES ;++$u) {
if ($in{'file-to-upload-$u'}) { $found = 1; }
}
if ($found) { do something here }

it isn't working. i googled some perl info and it seems this isn't allowed. is there some way to accomplish what i want?
Quote Reply
Re: [delicia] incrementing a variable In reply to
Hi,

Not sure if this is what you are after?


Code:
my ($found, $num_files, $u);
for ($u=1; $u <= $MAXIMUM_FILES; $u++) {
if ($in{"file-to-upload-${u}"}) { $found = 1; }
}
if ($found) { do something here }

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] incrementing a variable In reply to
thanks. that worked!
Quote Reply
Re: [delicia] incrementing a variable In reply to
Cool :)

Just FYI - all I did was change the ' to " (as ' will "escape" it, so '$test' would be interpreted directly as '$test' , not the value of $test) ... and also:

Code:
"foo-${test}"

Code:
${var}

..is a way to use a variable alongside something else. Mainly used in stuff like:

Code:
test$foosomething

where the variable is $foo, we would use:

Code:
test${foo}something

Just thought I'd give a bit more info on it :)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] incrementing a variable In reply to
you also but curly braces around the $u -- was that important?
Quote Reply
Re: [delicia] incrementing a variable In reply to
You probably could have got away with:

Code:
$in{"file-to-upload-$u"}

(i.e just double quotes, instead of single)

...but I always prefer to put them in { } out of habit, as it helps you read the code better (so you know thats the variable)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!