DingoBaby has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to get XBase to work and I used the code the came right off the xbase.html but I cant get it to work correctly. It is supposed to update a certain field when another field equals x (ie. I want to update field TEST with a value when LASTNAME equals Smith):
use Xbase; my $table = new XBase "live.dbf" or die Xbase->errstr; for (0 .. $table->last_record) { my ($deleted,$lastname) = $table->get_record($_,"LASTNAME"); die $table->errstr unless defined $deleted; next if $deleted; $table->update_record_hash($_,"TEST"=>"It works") if $LASTNAME + == "Smith"; }
Right now, with the code as it is..The TEST field is being updated for all records instead of only the records with LASTNAME = Smith. Many thanks for any suggestions you might. Im sure Im over looking something simple but Im just starting with Perl and I tend to miss things. Thank you, Todd

Replies are listed 'Best First'.
Re: Problem with using XBase
by VSarkiss (Monsignor) on Jan 25, 2002 at 01:22 UTC

    It looks like just the kind of thing use strict would have caught: you're assigning a value to $lastname (lowercase) and then testing $LASTNAME (uppercase).

    Also, if you're testing strings for equality, you should be using eq, not ==. In sum, your line should look like this:

    $table->update_record_hash($_, TEST => "It works") if $lastname eq "Smith";

    HTH

      Thank you..It works splendidly now. What is the "use strict" you referred to? I am very grateful for your assistance. Sincerely, Todd

        You're welcome. The use strict I referred to brings in the strict Perl module (technically, it's a pragma) into your program, which restricts it from doing certain things. You can read about it here or here.

        In a (very simplified) nutshell, it does three things:

        1. It disallows using variables which haven't been declared in some fashion, such as with my, our, use vars, etc. This would've caught your upper/lower case problem since there was no declaration for $LASTNAME.
        2. It generates an error if you use a bareword (i.e., an identifier with no type symbol like $, @, and %).
        3. It prevents you from using symbolic references. That's a bit of an advanced topic; at this stage you should just avoid them unquestioningly. ;-)
        It's the collective wisdom of the Perl community that any script longer than a few lines should always begin with use strict. I heartily agree.

Re: Question about XBase
by Zaxo (Archbishop) on Jan 25, 2002 at 06:19 UTC

    XBase.pm has an uppercase 'B'.

    After Compline,
    Zaxo