Re: Golf: Replacing part of a string with "*" (lvalue substr)
by tye (Sage) on Aug 18, 2006 at 15:44 UTC
|
my $string= '62880147894644072';
substr( $string, 0, -4 ) =~ tr//*/c;
print $string, $/;
Note that I substituted my own credit card number for yours in my test code. ;)
Update: ++ to Sidhekin for out golfing me while I was previewing. ( I like this solution because I find it clear, not because it involves fewer keystrokes. :)
| [reply] [d/l] |
|
|
| [reply] |
Re: Golf: Replacing part of a string with "*"
by jwkrahn (Abbot) on Aug 18, 2006 at 15:48 UTC
|
$ perl -le'
my $string = q/5424123412345678/;
$string =~ s/.(?=.{4})/*/g;
print $string;'
************5678
Update:
$ perl -le'
my $string = q/5424123412345678/;
$string =~ s/(?<=.{4}).(?=.{4})/*/g;
print $string;'
5424********5678
| [reply] [d/l] |
Re: Golf: Replacing part of a string with "*"
by Sidhekin (Priest) on Aug 18, 2006 at 15:43 UTC
|
29, counting the semicolon. :-)
my $string = '5424123412345678';
substr($string,0,-4)=~y//*/c;
print $string;
print "Just another Perl ${\(trickster and hacker)},"
The Sidhekin proves Sidhe did it!
| [reply] [d/l] [select] |
Re: Golf: Replacing part of a string with "*"
by Velaki (Chaplain) on Aug 18, 2006 at 15:54 UTC
|
echo "1234567890123456" |
perl -lne 'print"\*"x(length()-4),scalar reverse unpack "A4",reverse'
gratias vobis ago valeteque -v.
"Perl. There is no substitute."
| [reply] [d/l] |
Re: Golf: Replacing part of a string with "*"
by explorer (Chaplain) on Aug 18, 2006 at 15:25 UTC
|
$string =~ s/^\d{12}/'*' x 12/e; # ************5678
| [reply] [d/l] |
|
|
If the OP wanted to hardcode the length, he could have done
substr($string,0,-4,'*'x12);
Much clearer. | [reply] [d/l] |
Re: Golf: Replacing part of a string with "*"
by ikegami (Patriarch) on Aug 18, 2006 at 15:28 UTC
|
my $string = '5424123412345678';
substr$_,0,-4,'*'x(length()-4)for$string; # 41
print $string;
my $string = '5424123412345678';
s/(?!.{0,4}$)./*/g for$string; # 30
print $string;
Update: More readable than the previous:
my $string = '5424123412345678';
$string=~s/(?!.{0,4}$)./*/g; # 28
print $string;
| [reply] [d/l] [select] |
|
|
Here's 25:
$string=~s/.(?=....)/*/g;
| [reply] [d/l] |
|
|
| [reply] [d/l] |
|
|
Re: Golf: Replacing part of a string with "*"
by explorer (Chaplain) on Aug 18, 2006 at 15:30 UTC
|
$string = '*' x 12 . substr($string,-4); # ************5678
| [reply] [d/l] |
Re: Golf: Replacing part of a string with "*"
by BrowserUk (Patriarch) on Aug 18, 2006 at 15:23 UTC
|
printf "%-12.12s****\n", '5424123412345678';;
Update: Hmm. Misread your code. I thought you were obscuring the last 4 digits.
This will do the first twelve for a score of ...um...either 0 or -30-somthing :)
[0] Perl> printf "************%4.4s\n", '5424123412345678';;
************5424
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] [select] |
|
|
Still not right. It should be printing the *last* four (************5678).
| [reply] |
|
|
printf "************%s\n", substr('5424123412345678', -4);;
************5678
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] |
Re: Golf: Replacing part of a string with "*"
by explorer (Chaplain) on Aug 18, 2006 at 15:36 UTC
|
substr($string,rand length $string,1,"*") foreach 1..12; # 5****23**
+*34**7*
Update: No guarantee included :-) | [reply] [d/l] |
|
|
That's bad for readability (harder for the user to figure out which one of his cards was used), and it's bad security (since the number could be obtained over time or if multiple retailers did this). And of course, there's are times when two, one and even no digits are hidden.
| [reply] |
|
|
| [reply] |
|
|
Actually a couple issues with this, 1) not guaranteed to use any asterisks (usually will), 2) since they are different each time with enough receipts you could get the whole number.
- Ant
- Some of my
best work - (1 2 3)
| [reply] |