LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-13-2003, 03:07 PM   #1
inkedmn
LQ Newbie
 
Registered: Sep 2001
Location: Southern California, USA
Distribution: Debian unstable :)
Posts: 22

Rep: Reputation: 15
a first-timer perl question...


ok, so i'm toying with perl a bit and i'm writing a little isbn verifier app (something i write in just about every language i learn). basically, it takes a command line argument of an isbn string (like "123-3245-789X", or whatever), and, through a series of computations, determines if the number is valid...

anyway, here's my code at this moment:
Code:
#!/usr/bin/perl -w
use strict;

# perl ISBN verifier - my first perl attempt

my $sum1 = 0;
my $sum2 = 0;

sub isValid {
    my $sum = shift;
    return ($sum % 11 == 0);
}

sub getSums {
    my $isbn = shift;
    $isbn =~ s/[^\dXx]//;
    foreach(split $isbn) {
        if ($_ == "X" || $_ == "x") {
            $sum1 += 10;
        } else {
            $sum1 += $_;
        $sum2 += $sum1;
        }
    }
}

my $num = $ARGV[1] or die "command line args are wrong";
getSums($num);
if (isValid($sum2)) {
    print "Valid\n";
} else {
    print "Invalid\n";
}
and, when i run it, i get this:
Code:
<|inkedmn@skank|~/code/perl/code>% perl practice.pl 12345678X
command line args are wrong at practice.pl line 27.
my major trouble is grabbing the command-line arguments correctly (and i'm not sure if there are type issues), could somebody please tell me what i'm doing wrong?

thanks!

Last edited by inkedmn; 07-13-2003 at 03:08 PM.
 
Old 07-13-2003, 03:44 PM   #2
Mathieu
Senior Member
 
Registered: Feb 2001
Location: Montreal, Quebec, Canada
Distribution: RedHat, Fedora, CentOS, SUSE
Posts: 1,403

Rep: Reputation: 46
Change $ARGV[1] to $ARGV[0]

The first argument is located at [0] of the array (@ARGV)
 
Old 07-13-2003, 04:56 PM   #3
inkedmn
LQ Newbie
 
Registered: Sep 2001
Location: Southern California, USA
Distribution: Debian unstable :)
Posts: 22

Original Poster
Rep: Reputation: 15
yeah, found that right after i posted

python's equivalent is sys.argv which includes the name of program file, so [1] would be the first ACTUAL argument.

having some trouble with a couple other things, i'll post here if i can't figure them out

thanks!
 
Old 07-14-2003, 10:03 AM   #4
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
For a comparative example, here is my "version" of the isbn number validator. (=

Code:
#!/usr/bin/perl
use strict;
use warnings;

# subroutine prototyping allows us to define subs and what
# they are expected to receive (and perl will give an error,
# usually, if it is called incorrectly)
#
sub isValid($);

#  I've started doing this for the main loop of my scripts lately, to
# keep it inside of a block, for variable scoping, so we don't have
# globals.
#
#MAIN
{
  @ARGV or die "Usage:\n\n\t$0 <isbn_number> <...>\n";
  # test as many numbers as they wish to pass in
  #
  for my $isbnNumber(@ARGV) {
    print isValid($isbnNumber) ? "Valid\n" : "Invalid\n";
  }
  exit;
}

# I combined the sum and valid parts
#
sub isValid($)
{
  # double error checking to make sure that the sub gets called
  # correctly
  #
  my($isbn) = shift || die "isValid: expected isbn number, received nothing\n";
  my($sum)  = 0;
  my($digit);

  $isbn =~ s/[^0-9xX]//g;
  length($isbn) == 10 or return 0;

  # instead of split'ing, pull out each char individually w/ substr
  #
  for my $loc(1..10) {
    $digit = substr($isbn,$loc-1,1);
    $sum += ($digit =~ /[Xx]/ ? 10 : $digit) * $loc;
  }

  return ($sum % 11 == 0);
}
Anyway, that's my take. (=
 
Old 07-14-2003, 03:25 PM   #5
inkedmn
LQ Newbie
 
Registered: Sep 2001
Location: Southern California, USA
Distribution: Debian unstable :)
Posts: 22

Original Poster
Rep: Reputation: 15
ok, so i pretty much got it working (i'll post the code when i get home)...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Window VS Linux : timer question chanwing Programming 13 04-05-2005 04:27 AM
GNUChess Timer Question W0bbles Linux - Games 1 03-11-2005 06:00 AM
xchat question, timer moger Linux - Software 0 02-19-2004 12:46 PM
Hello All...First timer here....Have a question. N00B 4EVER Linux - Newbie 14 11-15-2003 10:24 AM
First timer question on chroot timna Linux From Scratch 1 07-08-2003 01:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration