| [reply] [d/l] |
Strip this down to a short CGI script that exhibits the failure, and post it for us to look at.
| [reply] |
This is due to the scalar nature of the variable you are using to store the number, (ie. PERL thinks it's a number not a string!), try padding with an alpha. | [reply] |
The short answer is take rob_au's (s)printf recommendation. The whole scalar is-a scalar is-a scalar (i.e., integer <=> float <=> string) thing has probably bitten all of us at one point or another. (s)printf can go a long way toward setting things right, when nothing else seems to work.
Be aware that, if you use CGI.pm to retrieve your script's parameters, the value returned by param() will be precisely what the user entered, warts (leading zeroes) and all. It is what happens after this point that determines whether (and when) Perl begins to treat your string of digits as a number. Sometimes it helps to step through the code with the perl debugger to see where things change.
(For a CGI script, you can obtain an appropriate command line for the debugger from your browser by changing the form's retrieval METHOD to GET, instead of POST. Then, after submitting your form, go to your browser's location bar and copy everything after the question mark (as in, http://your.server.domain/path/mycgiscript.cgi?...), not including the question mark istelf, and paste it onto the end of a command line at the prompt, so that it looks something like this: perl -d mycgiscript.cgi param1=val1¶m2=val2. Then step through the script to the location of the problem. Remember, you're not necessarily interested in *debugging* the script--although it's possible--but rather in determining at when and why a particular variable's value starts being treated as a numerical value.)
When dealing with strings that happen to look like numbers (e.g., zip codes), ensure that they are treated as strings by avoiding (deliberate or otherwise) numerical manipulations. You must be careful to examine what you do with the values subsequent to retrieving them; you may be implicitly forcing Perl to treat them as numbers (which is why dws asked to see your code).
It's important to do this analysis, because once your awareness of how/when/why Perl differentiates between numbers and strings reaches a critical mass, you may find yourself slapping your forehead, leaping up out of your chair and yelling, "Aha!"
Or maybe not.
If you cannot locate and correct where you might be (implicitly or otherwise) treating your fields as numbers, using one of sprintf or printf, to gather and format the three phone number fields into a single output value is perhaps a simpler solution.
dmm
You can give a man a fish and feed him for a day ...
Or, you can teach him to fish and feed him for a lifetime
| [reply] [d/l] [select] |
Opened a new node to examine this leading 0 thing a little deeper.
| [reply] |
This is a hack, but if you check the length of the field
and it's too short, prepend a zero:
while ( length $field2 < 4 ) { $field2 = "0" . $field2; }
Update Keep in mind that by doing this you can,
for instance, add to $field2, and still keep the leading
zero. Since I'm new, at least comment on why this node is
bad... | [reply] [d/l] |
You asked "comment on why this node is
bad...", and i think it would help you to know. From what i can tell, the reason people might down vote this is because perl has a set of internal functions which do the same thing much faster. sprintf and printf. The post prior to yours points this out. While i am sure most of us at some point have written code similar to yours, i imagine people down-voted it because the post prior haad a faster, cleaner solution. Don't let this discourage you ... read up on (s)printf for the next time ... this is how we all learn it.
$ perl -e 'do() || ! do() ;'
Undefined subroutine &main::try
| [reply] [d/l] |
Thanks, that's just what I was looking for. I have never used sprintf outside of C, and had not even thought of it in perl. I don't mind the '-' vote as much as not knowing why... Thanks.
| [reply] |