Link to home
Start Free TrialLog in
Avatar of hadrons
hadrons

asked on

Help improving the output using the Perl Business::ISBN Module

This is the script I wrote:

#!/usr/bin/perl

use strict;
use Encode qw(encode decode);
use Time::HiRes qw(gettimeofday);
use File::Copy;
use Business::ISBN qw( valid_isbn_checksum );  


my $t0 = gettimeofday;

##

system ("echo processing part four of five \.\.\.");
our @files = glob("ean.txt");      
foreach my $file(@files) {

                      open FILE, '<:encoding(UTF-8)', $file or warn "Can't open $file: $!";  
                      open PARSED, '>:encoding(UTF-8)', ($file . "_invalid.txt") or warn "Cannot open file for write: $!";  

while (<FILE>) {

foreach ($_) {

print $_ . valid_isbn_checksum($_) . "\n";


}

print PARSED;
}
}


close FILE;
close PARSED;

And it compiles with no problem. The output that's sent to the screen is:

$ perl Edit3
processing part four of five ...
9781481412063
1
9781442369405
1
9781476762975
1
97822222211111

The valid_isbn_checksum() function returns 1 if the ISBN is valid and 0 or undef if the ISBN is invalid. Of these 4 ISBNs, the first three of them are valid and the last one isn't.

And the file output has just a print out of the ISBN:

9781481412063
9781442369405
9781476762975
9782222221111

I would like just to see just the invalid ones and having the file output that would return the invalid number and "is invalid", like "9782222221111 is invalid"; thanks
Avatar of ozo
ozo
Flag of United States of America image

print "$_ isinvalid\n" unless valid_isbn_checksum($_);
Avatar of hadrons
hadrons

ASKER

The results stay the same, the only difference is that the output didn't reach the screen, but in the file all the ISBN printed whether they were invalid or not.

I even tried adding this:

if (!/1/) {

print $_;
}

And there was no change in the results
SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hadrons

ASKER

It's clear from my feedback that I'm not a trained programmer, but I do use Perl heavily because its so useful and I'm largely self-taught. I tend to try different things even after submitting a question and if some of the practices seem odd its because I tend to fall back on what previously worked.

I did rework the script using both of your recommendations and it works just how I want it:

#!/usr/bin/perl

use strict;
use Encode qw(encode decode);
use Time::HiRes qw(gettimeofday);
use File::Copy;
use Business::ISBN qw( valid_isbn_checksum );  


my $t0 = gettimeofday;

##

print ("echo processing part four of five \.\.\.");
my @files = glob("ean.txt");      
foreach my $file(@files) {

                      open FILE, '<:encoding(UTF-8)', $file or warn "Can't open $file: $!";  
                      open PARSED, '>:encoding(UTF-8)', ($file . "_invalid.txt") or warn "Cannot open file for write: $!";  

my @eans = <FILE>;
foreach my $eans (@eans){
print "$eans is invalid\n" unless valid_isbn_checksum($eans);
print PARSED "$eans is invalid\n" unless valid_isbn_checksum($eans);
}
}
close FILE;
close PARSED;

I do need the glob function because I will be working with files with a specific pattern (I just used eans.txt because I wanted just one test)