Adding Form elements

D

dbmeyers23

All,

I have a form with about 150 fields that I would like to have added
together automatically and the value placed into a new variable.
Below, you will see my @list. Below that, I'm going through @list and
only printing those values which have data (thanks to google.groups).

What I would like to do now, and I'm struggling a bit to do is add
these quantities together....so anywhere I see "addquantity", I would
like to keep adding the values until done..then place them in a new
variable. Any hints??

my @list = (

[addzip2 => 'Additional Zip 2' ],
[addquantity2 => 'Additional Quantity 2' ],
[addzip3 => 'Additional Zip 3' ],
[addquantity3 => 'Additional Quantity 3' ],
[addzip4 => 'Additional Zip 4' ],
[addquantity4 => 'Additional Quantity 4' ],
[addzip5 => 'Additional Zip 5' ],
[addquantity5 => 'Additional Quantity 5' ],
[addzip6 => 'Additional Zip 6' ],
[addquantity6 => 'Additional Quantity 6' ],
);

for (@labels) {

if ( $query->param( $$_[0] ) ) {

print "$$_[1]: ", $query->param( $$_[0] ), "<br>";
print INT_EMAIL "$$_[1]: ", $query->param( $$_[0] ), "\n";

}
}
 
D

dbmeyers23

I'm fully aware of the "+" operator....but I'm struggling with the
syntax of adding the params that only have data in them. I'll tinker
some more.
 
J

Jürgen Exner

All,

I have a form with about 150 fields that I would like to have added
together automatically and the value placed into a new variable.
Below, you will see my @list. Below that, I'm going through @list
and only printing those values which have data (thanks to
google.groups).

What I would like to do now, and I'm struggling a bit to do is add
these quantities together....so anywhere I see "addquantity", I would
like to keep adding the values until done..then place them in a new
variable. Any hints??

my @list = (

[addzip2 => 'Additional Zip 2' ],
[addquantity2 => 'Additional Quantity 2' ],

Sorry, I think you lost me. Is this an array of references to one-element
arrays of references to one-element hashes?

[...]
for (@labels) {

Where does @labels come from? You don't define it anywhere.
if ( $query->param( $$_[0] ) ) {
print "$$_[1]: ", $query->param( $$_[0] ), "<br>";
print INT_EMAIL "$$_[1]: ", $query->param( $$_[0] ), "\n";


This $$_[1] looks suspiciously like a symbolic reference. Is that what you
meant to be?

Sorry, I don't see any reference to addquantity in your code. Neither from
you description nor from your code I can guess what you are trying to do.

jue
 
X

xhoster

All,

I have a form with about 150 fields that I would like to have added
together automatically and the value placed into a new variable.

You have that in the wrong order. Make a new variable, and use it to
add up the things you want to add up. I guess you could use
List::Util::sum along with map and grep to avoid having to predeclare your
newv variable, but that seems rather silly.
Below, you will see my @list. Below that, I'm going through @list

You are not going through @list, you are going through @labels, whatever
that is.

and
only printing those values which have data (thanks to google.groups).

What I would like to do now, and I'm struggling a bit to do is add
these quantities together....so anywhere I see "addquantity", I would
like to keep adding the values until done..then place them in a new
variable. Any hints??
....

my $newvariable;
for (@labels) {

if ( $query->param( $$_[0] ) ) {

print "$$_[1]: ", $query->param( $$_[0] ), "<br>";
print INT_EMAIL "$$_[1]: ", $query->param( $$_[0] ), "\n";

$newvariable+=$$_[1] if $$_[0] =~ /addquantity/;
 
A

Alan Mead

All,

I have a form with about 150 fields that I would like to have added
together automatically and the value placed into a new variable.
Below, you will see my @list. Below that, I'm going through @list and
only printing those values which have data (thanks to google.groups).

What I would like to do now, and I'm struggling a bit to do is add
these quantities together....so anywhere I see "addquantity", I would
like to keep adding the values until done..then place them in a new
variable. Any hints??

My advice would be to post more code and explain the code. I don't
understand what you are doing.

But I do have some thoughts.

I wouldn't iterate over all the parameters, I would want a list of the
field names, call it "my @fieldnames". Then:

my $newvalue=0;
foreach my $field (@fieldnames) {
next unless (param($field));
next unless ($field =~ 'addquantity');
my @vals = param($field);
foreach my $v (@vals) {
$newvalue += $v;
}
}

Since I don't understand what you are doing, I don't know if this does
what you want but it (1) declares a new variable and initializes it.
That's not necessary but if you use warnings, I think it suppresses a
warning. Then (2) you iterate over your field names. The reason this is
preferable is that it's more secure. You skip fields which are blank (3)
or which do not contain the string 'addquanity' in their field names (4).
The fifth line captures values in the list @vals. You wouldn't need a
list if you gave each field a unique name which is how most people do
it. The remaining lines accumulate the values.

If you refuse to create @fieldnames yourself, this code can be made to
walk the entire parameter list by saying "my @fieldnames = param();"

Hope this helps.

-Alan
 
D

dbmeyers23

Thanks all, I realize my posting didn't make a whole lot of sense after
reading....but,

I ended up doing this, and it worked fine.

$totals = $totals + $query->param($$_[0]);
print "$totals is total";

I realize after reading the posts, that I probably could have done this
much easier...but hey, I'm fairly new to perl....thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top