in reply to Simple formatting question for credit card number

Anonymous Monk,
Because I am lacking in creativity, I will give you the first thing (and probably not the best thing) that came to mind).
#!/usr/bin/perl -w use strict; my $number = '4744760012394122'; if ($number =~ /^(\d{4})(\d{4})(\d{4})(\d{4})$/) { $number = "$1 $2 $3 $4"; } else { print "Unable to convert $number\n"; }
I hope that wasn't your real credit card number.

Cheers - L~R

Replies are listed 'Best First'.
Re: Re: Simple formatting question for credit card number
by liz (Monsignor) on Aug 12, 2003 at 20:18 UTC
    I would use:
    $number =~ s#(.{4})#$1 #g;

    There should be a quick way doing this with unpack(), but the heat just got to me... ;-)

    Liz

    Update
    Hmmm... that puts a space at the end. which may or may not be a problem. And it doesn't check for digits. So I guess this would be better:

    $number =~ s#(\d{4})(?=\d)#$1 #g;
    or:
    $number = "@{[unpack '(A4)*', $number]}";
    but that is heading towards obfuscation ;-) (thanks tedrek!)
      print join " ", unpack("A4A4A4A4", $number)