Postcode help!
Moderator: General Moderators
Postcode help!
Hi all,
Im currently working on a piece of programming using perl, im using regex to manipulate incoming postcodes. Once entering the postcode a format is also used i.e. U for the postcode to be outputted as uppercase, and format=L for the output to be all lowercase. The problem is i am programming using if and ifels commands and it doesnt seem to work.
Any help would be great
rgds
Sami P
Im currently working on a piece of programming using perl, im using regex to manipulate incoming postcodes. Once entering the postcode a format is also used i.e. U for the postcode to be outputted as uppercase, and format=L for the output to be all lowercase. The problem is i am programming using if and ifels commands and it doesnt seem to work.
Any help would be great
rgds
Sami P
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
CGI or command line? I don't really understand what you're asking and it's beeen a while since I wrote any perl (ifels? you mean elsif?)...
EDIT | Got bored, tested it and fixed the syntax errors 
Code: Select all
#!/usr/bin/perl
print "e;Enter postcode: "e;;
$postcode = <STDIN>; #Take input from STDIN
trim;
if ($postcode =~ /їa-z]{1,2}\d{1,2}\s*\dїa-z]{2}/i) #That's UK postcode
{
print "e;\nEnter U for uppercase or L for lowercase: "e;;
$case = <STDIN>;
trim;
if ($case == "e;u"e; || $case == "e;U"e;)
{
print uc($postcode)."e;\n"e;; #Why?
}
elsif ($case == "e;l"e; || $case == "e;L"e;)
{
print lc($postcode)."e;\n"e;;
}
else
{
print "e;You didn't enter U or L\n"e;;
}
}
else
{
print "e;That postcode isn't valid\n"e;;
}Hi thanks for the replys,
So far i have got some bit working, U = uppercase L = lowercase SP = a space between the postcode, if it is entered together and NS = no space if the postcode is entered with a space.
Some of aspects work and some are just baffeling me in the way they are not working. An example being the if a post code is inputted with a space and the U format is entered in returns an invalid output??
I was wondering if anyone knoew any simpler way to introduce the different formats rather than using the if and elsif beginnings?
any help would be appreciated
thanks
sami p
So far i have got some bit working, U = uppercase L = lowercase SP = a space between the postcode, if it is entered together and NS = no space if the postcode is entered with a space.
Some of aspects work and some are just baffeling me in the way they are not working. An example being the if a post code is inputted with a space and the U format is entered in returns an invalid output??
Code: Select all
#!/usr/bin/perl
use strict;
use CGI ':standard';
my ($postcode, $format, $up, $down, $cut, $lft, $rgt, $rigt);
$postcode = param('postcode');
$format = param('format');
$up = param '';
$down = param '';
$up = uc($postcode);
$down= lc($postcode);
#$cut= chop($postcode);
$lft= substr($postcode, 0, 3);
$rgt= substr($postcode, 3, 4);
$rigt = substr($postcode, 3, 7);
print "e;content-type: text/html\n\n"e;;
if ( $postcode =~ /\D{2}\d{1,2}\s\d\D{2}/ && $format eq "e;NS"e; || $format eq "e;ns"e;) {
$postcode =~ s/ //g;
print "e;$postcode\n"e;;
}
elsif ( $postcode =~ /\D{2}\d{1,2}\s*\d\D{2}/g && $format =~/U/) {
print "e;$up\n"e;;
}
elsif ( $postcode =~ /\D{2}\d{1,2}\s\d\D{2}/ || $format eq "e;SP"e; || $format eq "e;sp"e;) {
print "e;$lft $rgt\n"e;;
}
elsif ( $postcode =~ /\D{2}\d{1,2}\s*\d\D{2}/ && $format =~/L/) {
print "e;$down\n"e;;
}
elsif ( $postcode =~ /\D{2}\d{1,2}\s*\d\D{2}/ || $format =~/ /) {
print "e;$postcode\n"e;;
}
else
{
print "e;Invalid Data\n"e;;
}any help would be appreciated
thanks
sami p
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
if..elsif..else makes perfect sense to me. I wouldnt change that. The only other way to *sometimes* do things like that is to use a switch() condition but that wont work here because of the conditions in each of your steps here.
<side note>
Thanks for reminiding me how nice a language perl is again.
(and that it's "eq" not "==" when dealing with string literals
)
</side note>
Your regex could certainly be improved however.... they will match a postcode yes but they dont represent the actual pattern a postcode takes (we are talking about UK postcodes I assume from looking at your code?).
Might be an idea to drop the
$format eq "NS" || $format eq "ns"
and replace it with
lc($format) eq "ns"
Also, this could explain your problem with getting "Invalid input"...
$format =~/L/
It's case sensitive (and why do a perl re on a single character?)..
Try replacing it with:
lc($format) eq "l"
I can probably post a method that utilises regex a bit better though (backreferences rather than using substr() since you have the patterns available) 
<side note>
Thanks for reminiding me how nice a language perl is again.
(and that it's "eq" not "==" when dealing with string literals
</side note>
Your regex could certainly be improved however.... they will match a postcode yes but they dont represent the actual pattern a postcode takes (we are talking about UK postcodes I assume from looking at your code?).
Might be an idea to drop the
$format eq "NS" || $format eq "ns"
and replace it with
lc($format) eq "ns"
Also, this could explain your problem with getting "Invalid input"...
$format =~/L/
It's case sensitive (and why do a perl re on a single character?)..
Try replacing it with:
lc($format) eq "l"
Code: Select all
#!/usr/bin/perl
use strict;
use CGI ':standard';
my ($postcode, $format, $up, $down, $cut, $lft, $rgt, $rigt);
$postcode = param('postcode');
$format = param('format');
$up = param '';
$down = param '';
$up = uc($postcode);
$down= lc($postcode);
#$cut= chop($postcode);
$lft= substr($postcode, 0, 3);
$rgt= substr($postcode, 3, 4);
$rigt = substr($postcode, 3, 7);
print "e;content-type: text/html\n\n"e;;
if ( $postcode =~ /їa-z]{2}\d{1,2}\s\dїa-z]{2}/i && lc($format) eq "e;ns"e;) {
$postcode =~ s/ //g;
print "e;$postcode\n"e;;
}
elsif ( $postcode =~ /їa-z]{2}\d{1,2}\s*\dїa-z]{2}/i && lc($format) eq "e;u"e;) {
print "e;$up\n"e;;
}
elsif ( $postcode =~ /їa-z]{2}\d{1,2}\s\dїa-z]{2}/i || lc($format) eq "e;sp"e;) {
print "e;$lft $rgt\n"e;;
}
elsif ( $postcode =~ /їa-z]{2}\d{1,2}\s*\dїa-z]{2}/i && lc($format) eq "e;l"e;) {
print "e;$down\n"e;;
}
elsif ( $postcode =~ /їa-z]{2}\d{1,2}\s*\dїa-z]{2}/i || $format =~/^$/) {
print "e;$postcode\n"e;;
}
else
{
print "e;Invalid Data\n"e;;
}It has been ages since i've done some Perl, but i've been told the following 
Instead of using (show warnings)
use:
Instead of using (show warnings)
Code: Select all
#!/bin/perl -wCode: Select all
#!/bin/perl
use warnings;- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Ignore my statement about using a swicth too... it doesnt appear to work in perl lol...
You got me playing with it now
Same here timvw... I think i last touched it 3 years ago... best way to learn regex though
TIP: Add ^ and $ to the start and end of your patterns... you're letting invalid data in at the start and end the way you have it
Notice I added the parens... you can get everything out of those rather than using substr() 
You got me playing with it now
Same here timvw... I think i last touched it 3 years ago... best way to learn regex though
TIP: Add ^ and $ to the start and end of your patterns... you're letting invalid data in at the start and end the way you have it
Code: Select all
/^(їa-z]{2}\d{1,2})(\ *)(\dїa-z]{2})$/i
Last edited by Chris Corbyn on Thu Jul 28, 2005 6:17 pm, edited 2 times in total.
perl5.8 seems to support switch 
http://perl.active-venture.com/lib/Switch.html (woow, the first version i used was 4.x)
http://perl.active-venture.com/lib/Switch.html (woow, the first version i used was 4.x)
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Ah I have 5.8.5, the syntax must be different to what I thought cheers... i'll have a read up.
samip1983 unless your query is specifically over the regex then we dont really deal with perl (although you got me started now
)...
EDIT | Yes the syntax is not like that of PHP switch()... needs curlies { } and no colon :
samip1983 unless your query is specifically over the regex then we dont really deal with perl (although you got me started now
EDIT | Yes the syntax is not like that of PHP switch()... needs curlies { } and no colon :
Thanks for all your replys, at work at the moment so ill have a play with iut later......hopefully get it working, the main think thats annoying me is the spacing once ns or sp is entered as a format. it only works when there is a postcode like cv16sy, but doest work if the postcode is like cv116sy.
But i think the change in regex has made a difference, thanks.
cheers.....
But i think the change in regex has made a difference, thanks.
cheers.....
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Hi,
i wasnt aware you could access the postcode using lhs and rhs, this might be easier to use the spacing formats ns-no space sp-space.
i was wondering using the code below
why is there lc before the format, i know it means lower case but i need the case depending on what format the user enters.
thanks guys
i wasnt aware you could access the postcode using lhs and rhs, this might be easier to use the spacing formats ns-no space sp-space.
i was wondering using the code below
Code: Select all
elsif ( $postcode =~ /їa-z]{2}\d{1,2}\s*\dїa-z]{2}/i && lc($format) eq "e;l"e;) {print "e;$down\n"e;;thanks guys
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Well it's lowercase of whatever the user enters comparing against lowercase "l" or "u"... so if upper or lowercase L or U is typed then it still matches.
for example:
Saves doing:
for example:
Code: Select all
$foo = "e;BAR"e;;
if (lc($foo) eq "e;bar"e;) print $foo;Code: Select all
$foo = "e;BAR"e;;
if ($foo eq "e;BAR"e; || $foo eq "e;bar"e;) print $foo;