Gregoire Hostettler wrote:
Hi!

I do not know I am on the right mailing list.
If not, sorry for the burden.

To say thing briefly:
I am a programmer for some 25 years using various old and newer languages.
I have to write some small things in Perl.
It was fine and fast with some text analysis.

Now I have to write something using an in-memory "database", i.e. a SINGLE
table filled with records.

I am trying to simply add records then retriev them.
I already spent more than 20 hours for something which should take 30
minutes....

Seriousely considering making my customer change his mind and revert to
plain old C++
Just to be certain not to miss something simple, I attach a small part of my
very, very basic trials.

As you can see when running this small piece of "code", as soon as you push
a new record, all records already existing become
filled with the new pushed record and it is impossible to get any other...

Looks like Perl is not able to handle trivial data structure like an array
of records (or hashes).

Thank you for any help!


Caracal - G. Hostettler 6, ch. du Raidillon 1522 Lucens


My first bit of advise, don't pass around hashes. Pass references. Second don't declare the @table. Or rather declare it (with my) but don't initialize it. The assignment make a great comment, but you don't declare 'fields' in a hash. On the push in addrec push a reference.


        push @table, { %lrec};

It's just like an array of pointers in C. It an array of hash references (or pointers).

In getrec, you have an off by 1. $#table is the last used index in @table, not the size. It's 1 too small. Use scalar(@table to get the size or do this

        if (($recnum < 0) or ($recnum > ($#table) )) {

To get you hash back you'd need to dereference it.

        %lrec = %{$table[$recnum]}

Your statements to print the size should look more like:

        print "Added rec. tblsize = ", scalar(@table), "\n";

After all of this, you code is still going to be kind of ugly because you pass around the hash in list context.

try something like

        sub addrec { # takes a list of key value pairs
                push @table, { @_ }
        }

        getrec($){ # take an index, returns hash ref or undef on failure
                return $table[shift]; # gets undef() on failure
        }

Dan



Reply via email to