CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Issues fixing my old script

 



sh0nuff
New User

Oct 10, 2014, 12:09 PM

Post #1 of 8 (9381 views)
Issues fixing my old script Can't Post

Hey all, several years ago I wrote a script to find credit cards in files, well its been awhile and now I am need of this script again, the problem is I haven't written any scripts since I wrote this.. for some reason the script no longer works and I am also needing to add functionality.

If anyone can be of some help, it would be most appreciated.
I am also wanting to add a way for it to tell the location in the file where it found the number such as what line it's on and print the first 6 and last four out to a new file.

(This script doesn't have the new additions to it as I have no clue how to do it)

#!/usr/bin/perl -w
use strict;

my $filename = ("somefile.txt");
my $clog = ();
my $b1 = ();
my $b3 = ();
my $b5 = ();
my $b7 = ();
my $b9 = ();
my $b11 = ();
my $b13 = ();
my $b15 = ();
my $n1 = ();
my $n3 = ();
my $n5 = ();
my $n7 = ();
my $n9 = ();
my $n11 = ();
my $n13 = ();
my $n15 = ();
my $num = ();
my $status = ();
my $ccard = ();
my $foundcard = ();
my @LOGFILE = ();
my @GOODCARD = ();
my @BADCARD = ();

&readlogfile;
&searchlogfile;
&formatcard;
&good_or_bad;
&do_the_math;
&do_what_with_it;

sub readlogfile {
@ARGV = ("$filename") unless @ARGV;
while (<>) {
$clog = $_;
chomp @LOGFILE;
push (@LOGFILE, $clog);
}
}


sub searchlogfile {
chomp @LOGFILE;
foreach (@LOGFILE) {
if ( /(\d\d\d\d)[\-\s]?(\d\d\d\d)[\-\s]?(\d\d\d\d)[\-\s]?(\d\d\d\d)/) {
$ccard = "$_\n";
}
}
}

sub formatcard {
if ($ccard =~ /(\d\d\d\d)[\-\s]?(\d\d\d\d)[\-\s]?(\d\d\d\d)[\-\s]?(\d\d\d\d)/) {
$foundcard = "$1$2$3$4$5$6$7$8$9$10$11$12$13$14$15$16";
}
}

#print "$ccard\n";
sub do_the_math {
foreach ($foundcard) {


$n15 = ($15 * 2);
if ($n15 >= 10) {
$b15 = ($n15 - 9);
}
else{ $b15 = $n15;
}

$n13 = ($13 * 2);
if ($n13 >= 10) {
$b13 = ($n13 - 9);
}
else{ $b13 = $n13;
}

$n11 = ($11 * 2);
if ($n11 >= 10) {
$b11 = ($n11 - 9);
}
else{ $b11 = $n11;
}

$n9 = ($9 * 2);
if ($n9 >= 10) {
$b9 = ($n9 - 9);
}
else{ $b9 = $n9;
}

$n7 = ($7 * 2);
if ($n7 >= 10) {
$b7 = ($n7 - 9);
}
else{ $b7 = $n7;
}

$n5 = ($5 * 2);
if ($n5 >= 10) {
$b5 = ($n5 - 9);
}
else{ $b5 = $n5;
}

$n3 = ($3 * 2);
if ($n3 >= 10) {
$b3 = ($n3 - 9);
}
else{ $b3 = $n3;
}

$n1 = ($1 * 2);
if ($n1 >= 10) {
$b1 = ($n1 - 9);
}
else{ $b1 = $n1;
}
$num = ($b1 + $2 + $b3 + $4 + $b5 + $6 + $b7 + $8 + $b9 + $10 + $b11 + $12 + $b13 + $14 + $b15 + $16);
}
}
sub do_what_with_it {
if ($num % 10 == 0) {
push (@GOODCARD, $foundcard);
}
else{
push (@BADCARD, $foundcard);
}
}

sub good_or_bad {
if (@GOODCARD) {
print "I WORKY\n@GOODCARD";
}
if (@BADCARD) {
print "I NO WORKY\n@BADCARD";
}
}


Here are the errors I get, I think the main issue is where I am trying to pull the 16 digits out and put each one into a variable by $1$2$3 etc. which causes all the rest to fail because the number isn't being formatted correctly.

C:\Users\*\Documents>ccscripttest1.pl
Use of uninitialized value $5 in concatenation (.) or string at C:\Users\*\Documents\ccscripttest1.pl line 58, <> line 1.
Use of uninitialized value $6 in concatenation (.) or string at C:\Users\*\Documents\ccscripttest1.pl line 58, <> line 1.
Use of uninitialized value $7 in concatenation (.) or string at C:\Users\*\Documents\ccscripttest1.pl line 58, <> line 1.
Use of uninitialized value $8 in concatenation (.) or string at C:\Users\*\Documents\ccscripttest1.pl line 58, <> line 1.
Use of uninitialized value $9 in concatenation (.) or string at C:\Users\*\Documents\ccscripttest1.pl line 58, <> line 1.
Use of uninitialized value $10 in concatenation (.) or string at C:\Users\*\Documents\ccscripttest1.pl line 58, <> line 1.
Use of uninitialized value $11 in concatenation (.) or string at C:\Users\*\Documents\ccscripttest1.pl line 58, <> line 1.
Use of uninitialized value $12 in concatenation (.) or string at C:\Users\*\Documents\ccscripttest1.pl line 58, <> line 1.
Use of uninitialized value $13 in concatenation (.) or string at C:\Users\*\Documents\ccscripttest1.pl line 58, <> line 1.
Use of uninitialized value $14 in concatenation (.) or string at C:\Users\*\Documents\ccscripttest1.pl line 58, <> line 1.
Use of uninitialized value $15 in concatenation (.) or string at C:\Users\*\Documents\ccscripttest1.pl line 58, <> line 1.
Use of uninitialized value $16 in concatenation (.) or string at C:\Users\*\Documents\ccscripttest1.pl line 58, <> line 1.
Use of uninitialized value $15 in multiplication (*) at C:\Users\*\Docume
nts\ccscripttest1.pl line 67, <> line 1.
Use of uninitialized value $13 in multiplication (*) at C:\Users\*\Docume
nts\ccscripttest1.pl line 74, <> line 1.
Use of uninitialized value $11 in multiplication (*) at C:\Users\*\Docume
nts\ccscripttest1.pl line 81, <> line 1.
Use of uninitialized value $9 in multiplication (*) at C:\Users\*\Documen
ts\ccscripttest1.pl line 88, <> line 1.
Use of uninitialized value $7 in multiplication (*) at C:\Users\*\Documen
ts\ccscripttest1.pl line 95, <> line 1.
Use of uninitialized value $5 in multiplication (*) at C:\Users\*\Documen
ts\ccscripttest1.pl line 102, <> line 1.
Use of uninitialized value $3 in multiplication (*) at C:\Users\*\Documen
ts\ccscripttest1.pl line 109, <> line 1.
Use of uninitialized value $1 in multiplication (*) at C:\Users\*\Documen
ts\ccscripttest1.pl line 116, <> line 1.
Use of uninitialized value $2 in addition (+) at C:\Users\*\Documents\ccs
cripttest1.pl line 122, <> line 1.
Use of uninitialized value $4 in addition (+) at C:\Users\*\Documents\ccs
cripttest1.pl line 122, <> line 1.
Use of uninitialized value $6 in addition (+) at C:\Users\*\Documents\ccs
cripttest1.pl line 122, <> line 1.
Use of uninitialized value $8 in addition (+) at C:\Users\*\Documents\ccs
cripttest1.pl line 122, <> line 1.
Use of uninitialized value $10 in addition (+) at C:\Users\*\Documents\cc
scripttest1.pl line 122, <> line 1.
Use of uninitialized value $12 in addition (+) at C:\Users\*\Documents\cc
scripttest1.pl line 122, <> line 1.
Use of uninitialized value $14 in addition (+) at C:\Users\*\Documents\cc
scripttest1.pl line 122, <> line 1.
Use of uninitialized value $16 in addition (+) at C:\Users\*\Documents\cc
scripttest1.pl line 122, <> line 1.


BillKSmith
Veteran

Oct 10, 2014, 9:18 PM

Post #2 of 8 (9368 views)
Re: [sh0nuff] Issues fixing my old script [In reply to] Can't Post

Lines 57 and 58 are not compatible with each other. It is possible that very old versions of perl handled the error differently, but I believe that it was always an error. I strongly doubt that your script (as posted) ever 'worked'.

Sorry, I cannot help you correct it because I have no idea why you expect variables $5 through $16 to be initialized or what you expect perl to do if they are not.

The same argument applies to most of the errors you are reporting.
Good Luck,
Bill


Laurent_R
Veteran / Moderator

Oct 11, 2014, 3:59 AM

Post #3 of 8 (9365 views)
Re: [sh0nuff] Issues fixing my old script [In reply to] Can't Post

If this line:

Code
if ($ccard =~ /(\d\d\d\d)[\-\s]?(\d\d\d\d)[\-\s]?(\d\d\d\d)[\-\s]?(\d\d\d\d)/) {

gives a successful match, then it will populate only $1, $2, $3 and $4, each with a group of four digits. So that $5 to $16 are uninitialized, which is the error that you get on the next line of code:

Code
$foundcard = "$1$2$3$4$5$6$7$8$9$10$11$12$13$14$15$16";


Presumably what you want here is the 16 digits of the matched string, which you could do as follows:

Code
$foundcard = "$1$2$3$4";


But that would not help for some code further down the stream, where you appear to be using $15, $13, etc. individually.

I find it very hard to believe that this script ever worked.

The script is generally poorly written, most of the code is just against all the good practices of the modern uses of Perl. If I were given such code to maintain, I would rewrite it completely. For example, I would be using arrays or hashes instead of variables having the same name with just a different number, which would avoid useless code repetition and cut down the code toprobably one third of its current length.

I can't say much more at this point, not knowing what this code is supposed to do.


(This post was edited by Laurent_R on Oct 11, 2014, 3:59 AM)


sh0nuff
New User

Oct 11, 2014, 10:35 AM

Post #4 of 8 (9318 views)
Re: [Laurent_R] Issues fixing my old script [In reply to] Can't Post

yeah like I said I wrote it several years ago, it was my second script.

I have a ton of different versions of it as I worked through its problems..one of them worked but sadly I lost my backups and this was the only one I had left.



I see what your saying about it dropping all 16 digits into groups of 4 which is why my math isn't working as it also sees it as 4 variables instead of 16.. The math is actually the luhn check. I'm sure there are plenty of ways to write this script better..but again like I said it was my second script, never touched perl before or after lol. I do greatly appreciate you all looking at it! Thanks again!


Laurent_R
Veteran / Moderator

Oct 12, 2014, 11:22 PM

Post #5 of 8 (8689 views)
Re: [sh0nuff] Issues fixing my old script [In reply to] Can't Post

OK, now that you said that this is a Luhn code calculation, I know what this code is aiming at and I think that I should probably be able to help you. Not now from my mobile device, but possibly next evening.


Laurent_R
Veteran / Moderator

Oct 13, 2014, 5:25 AM

Post #6 of 8 (8608 views)
Re: [sh0nuff] Issues fixing my old script [In reply to] Can't Post

This is some code that should compute the Luhn key for a number:


Code
use strict; 
use warnings;

print luhn($_), "\n" for qw / 7992739871 972-487-086 225640055551 103747048426/;

sub luhn {
my $nsc = shift;
my $sum;
$nsc =~ s/\D//g; # removing non digits (dashes, etc.)
my @digits = reverse split //, $nsc;
for (my $i = 0; $i <= $#digits; $i++) {
my $digit = $digits[$i];
if ($i % 2 == 0) {
$digit *= 2;
$digit -= 9 if $digit > 9;
}
$sum += $digit;
}
my $luhn = 10 - ($sum % 10);
$luhn = 0 if $luhn == 10;
$luhn = sprintf "%0d", $luhn;
return $luhn;
}



BillKSmith
Veteran

Oct 13, 2014, 7:23 AM

Post #7 of 8 (8573 views)
Re: [sh0nuff] Issues fixing my old script [In reply to] Can't Post

Perl includes a module for validating credit card numbers.

Refer: perldoc Regexp::Common::CC

I do not know if it is appropriate to your task, but you should have a look.
Good Luck,
Bill


Laurent_R
Veteran / Moderator

Oct 13, 2014, 8:15 AM

Post #8 of 8 (8556 views)
Re: [BillKSmith] Issues fixing my old script [In reply to] Can't Post

Yes, I also forgot to mention that there is a LUHN module: http://search.cpan.org/~tayers/Algorithm-LUHN-1.00/LUHN.pm.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives