in reply to Re^2: Simple formatting question for credit card number
in thread Simple formatting question for credit card number
#!/usr/bin/perl use strict; use warnings; my $x = "12345678"; my $y = "123456789"; sub dragonchild { my ($n, $s) = @_; $n = reverse $n; # Added negative lookahead $n =~ s/(.{4})(?!$)/${1}${s}/g; scalar reverse $n; } sub aristotle { my ($n, $s) = @_; scalar reverse join $s, (reverse $n) =~ /(....)/g; } foreach my $v ($x, $y) { print dragonchild($v, '-'), $/; print aristotle($v, '-'), $/; } ------ 1234-5678 1234-5678 1-2345-6789 2345-6789
Your code only works for card numbers that are a multiple of 4. Most department store cards are 11 digits and some, like AmEx, are moving to 17 or 19 digits. (I used to work for MC ... we had a similar function that I had to maintain in the app I worked on. It was written horribly with substr and the like.)
------
We are the carpenters and bricklayers of the Information Age.
The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6
Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Simple formatting question for credit card number
by Aristotle (Chancellor) on Aug 13, 2003 at 14:28 UTC |