Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
I'm still new to programming and have been working through Lerning Perl and LPORM in conjunction with the perldoc. While reading an article I came across about how credit card numbers work, I figured it was a decent opportunity to practice what I've been learning. I know this is basic code, but I was hoping that in offering it up, the monks would review it and tell me if I'm striking out syntactically/semantically and/or demonstrating ignorance of generally accepted standards/conventions. The code runs, and does what it's supposed to. I plan on modifying it later to include validation for 13,14, and 15 digit credit card numbers as well as possibly reading a list of numbers from a text file and writing only valid numbers to a new file. I wanted to wait on expanding the program until I was sure that I wasn't completely off track.
#!/usr/perl/bin #Simple program to check validity of 16 digit credit card numbers use strict; use warnings; sub checkNumber { my $in = shift; my @cardNum = @{$in}; my $i = $#cardNum - 1; while ($i >= 0) { $cardNum[$i] = $cardNum[$i] * 2; if ($cardNum[$i] >= 10) { $cardNum[$i] -= 9; } $i -= 2; } my $total = 0; $total += $_ foreach (@cardNum); if ($total % 10 == 0) { return 1; } else { return 0; } } sub cardInfo { my $cardNum = shift; my $issuer; if ($cardNum =~ /^4/) { $issuer = "VISA" } if ($cardNum =~ /^5/) { $issuer = "MasterCard" } if ($cardNum =~ /^6/) { $issuer = "Discover" } print "\n Card Number: $cardNum\n"; print " Issuer: ", $issuer,"\n\n"; } print "Enter Number: "; chomp(my $cardNum = <STDIN>); if ($cardNum =~ /^[0-9]+$/) { my @number = split(//,$cardNum); if ($#number == 15) { if (checkNumber(\@number)) { cardInfo($cardNum); } else { print "\nInvalid Card Number\n"; } } else { print "\n16 digit cards only for now.\n"; } } else { print "\nInvalid Input.\n"; }

In reply to Code Review: Simple Credit Card Number Validation by marquezc329

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (4)
    As of 2014-11-28 07:01 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My preferred Perl binaries come from:














      Results (193 votes), past polls