Print

Print


On Wednesday, May 31, 2006 11:01 AM, Kevin Kierans
>Anyone willing to share a (preferably) perl 13 to 10 character
>and a 10 to 13 character conversion routine?

Below is something I use, in Perl, using Business::ISBN. It prints the 10
when given the 13, and then gives the correct check digit for the 13--so
entering '978' plus the 10 as-is would print the correct 13.


###################

use Business::ISBN;
use strict;
use warnings;
print "Please enter 13 digit EAN or ISBN (or q to quit): ";
while (my $line = <>) {
        chomp $line;
        last if ($line =~ /^q/);
        #remove all non-digit (or X) characters from line
        $line =~ s/[^0-9xX]//g;

        #skip lines with no ISBN/EAN number
        if ($line =~ /^\d{9}[\dX]$/) {
                my $is_valid = Business::ISBN::is_valid_checksum($line);
                if ($is_valid == 1) {
                print $line, " is valid\n";
                } #valid
                else {
                print $line, " has bad checksum\n";
                } #not valid
        } #line is 10 digits
        #unless line has ISBN already or is a non-digit line, convert ean to
isbn
        elsif ($line =~ /^\d{12}[\dX]$/) {
                my $isbn = Business::ISBN::ean_to_isbn($line);
                if ($isbn) {
                        print $isbn, " is the 10-digit ISBN\n";
                        my $good_ean = Business::ISBN::isbn_to_ean($isbn);
                        if ($good_ean) {
                                print $good_ean, " is the correct 13-digit
for this ISBN\n";
                        } #if good ean was found
                        else {
                                print "Failed to find EAN for that
supposedly valid ISBN\n";
                        } #something went wrong--what?
                } #isbn defined
                else {
                        print $line, " is not a valid ean string\n";
                } #invalid 13 digit
        }
        else {print "$line is an exception (not 10 or 13 digits)\n";}
        print "Please enter 13 digit EAN or ISBN (or q to quit): ";
} # while


########################


I hope this helps,
Bryan Baldus
[log in to unmask]
[log in to unmask]
http://home.inwave.com/eija