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

accessing total number of entries from a field 1

Status
Not open for further replies.

olivia

Programmer
Apr 10, 2000
8
US
Hi, <br>I have a inventory that has, several fields with identical names, the value is always 1. When someone checks three fields with the same name, output is <br>Pencils: 1, 1, 1<br>I want to have the total number inserted, in this case 3 so<br>Pencils: 3<br>I realize this is creating an array but I can't seem to access the number. I've tried<br>if ($Form{$field} =~ /\d/){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@var = ({$field});<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$var = @var;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print MAIL &quot;$field: $var\n&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}else{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print MAIL &quot;$field: $Form{$field}\n&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>but it never adds it up. All the variables are decoded<br>at the beginning<br>foreach $pair (@pairs) {<br>&nbsp;&nbsp;&nbsp;&nbsp;local($name, $value) = split(/=/, $pair);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$name =~ tr/+/ /;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack(&quot;C&quot;, hex($1))/eg;<br>&nbsp;$value =~ tr/+/ /;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack(&quot;C&quot;, hex($1))/eg;<br>Is this why it won't create an array? <br>Confused.
 
So, you're passing an input field into the CGI with at a name = 'Pencil' and a value = '1,1,1'.<br>And you want to know how many pencils there are....????<br>I believe you are lacking one small step....you need to split the string, '1,1,1', into elements when you stick them into your array, @var.<br><FONT FACE=monospace>#!perl<br>$field = 'Pencil';<br>$FORM{$field} = '1,1,1';<font color=red> # simulate input from web form</font><br>print &quot;$FORM{$field}\n&quot;;<br><font color=red># <i>split</i> usually looks like <i>split(//,$var);</i>, but the comma is a metachar<br># so it must be escaped with '\'. So,.....</font><br>@vars = split(/\,/,$FORM{$field});<br>$howMany = $#vars + 1;<font color=red># add one because array is counted from zero.</font><br>print &quot;$howMany\n&quot;;<br></font><br><br>You could do this with a pattern match also, but, I have tried to maintain some consistency with your approach.<br>'hope this helps.... <p> <br><a href=mailto: > </a><br><a href= > </a><br> keep the rudder amid ship and beware the odd typo
 
Great, that is exactly what was needed. I used pattern matching <br>if ($Form{$field} =~ /1\,/){<br>and it all adds up<br>Thanks so much!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top