Willy wrote:
> 
> i have a vexing problem for you....
> the following does work fine:::
> 
> [snip]
> 
> the output is as follows::
> ------------------------------------------------
> line one
> 
> line one line two line three line four line five line sixe line seven line
> 8 line 9
> 
> l11
> 
> l11 l12 l13 l14 l15 l16 l17 l18 l19

Are you discarding every tenth line on purpose or is this a bug in your
code?


> ------------------------------------------------
> 
> but, when i uncomment for DBM statemnents as shown here:::
> 
> #!/usr/bin/perl -w

You should use strict as well as warnings while developing your code.

use strict;


> dbmopen(%CARDREC, "testbase22", 0664)||
> die "can't dbmopen testbase with mode 0664";

perldoc -f dbmopen
       dbmopen HASH,DBNAME,MASK
               [This function has been largely superseded by the
               `tie' function.]

You should probably be learning to use tie instead of dbmopen.


> open (IN, "test.in")||
> die "can't open test.in";

You should include the $! variable in the error message so you know why
it failed.

open IN, 'test.in' or die "can't open 'test.in' $!";


> $index=0;
> foreach (<IN>){       $index++;

foreach reads the entire file at once into a list.  The normal way is to
read a line at a time with a while loop.  Using a while loop also gives
you access to the $. variable which means that you don't need the $index
variable.

>        ($index=1) if ($index>10);
>        chomp();
>        push(@card,$_) if ($index<10);
> 
>         if ($index==10){
>         $CARDREC{$card[0]} = [@card];
>         print "$CARDREC{$card[0]}[0]\n\n";
>         print "@{$CARDREC{$card[0]}}\n\n";
>         @card=();}}
> 
> dbmclose(%CARDREC);
> 
> i get the following output::
> ------------------------------------------------
> 
> Use of uninitialized value in concatenation (.) or string at
> ./cardbase-0.1.0.pl line 14, <IN> line 21.
> 
> Use of uninitialized value in concatenation (.) or string at
> ./cardbase-0.1.0.pl line 14, <IN> line 21.
> 
> ------------------------------------------------
> 
> after a lot of playing around... i think that my dbm syntax is
> right... so i am just plain confused :(

You are trying to store an array reference [ARRAY(0x12345678)] to a file
which changes the reference to a string, and once it becomes a string
can't be changed back to a reference.  You need to use a module like
Storable or join the values into a single string like this:

#!/usr/bin/perl -w
use strict;

dbmopen %CARDREC, 'testbase22', 0664
    or die "can't dbmopen testbase with mode 0664 $!";
open IN, 'test.in'
    or die "can't open 'test.in' $!";

my @card;
while ( <IN> ) {
    chomp;
    push @card, $_ if $. % 10;

    unless ( $. % 10 ) {
        $CARDREC{ $card[ 0 ] } = join "\0", @card;
        print "$card[0]\n\n";
        print "@{[ split /\0/, $CARDREC{$card[0]} ]}\n\n";
        @card = ();
        }
    }

dbmclose %CARDREC;

__END__



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to