Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Perl Advocacy > Re: Serious que...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 4 Topic 20 of 91
Post > Topic >>

Re: Serious question on using Perl or not...

by hursh@[EMAIL PROTECTED] (Dan Hursh) Jul 26, 2004 at 04:25 PM

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 @[EMAIL PROTECTED]
  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 @[EMAIL PROTECTED]
 { %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 
@[EMAIL PROTECTED]
 not the size.  It's 1 too small. Use scalar(@[EMAIL PROTECTED]
 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(@[EMAIL PROTECTED]
), "\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 @[EMAIL PROTECTED]
 { @[EMAIL PROTECTED]
 }
	}

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

Dan
 




 4 Posts in Topic:
Serious question on using Perl or not...
ghostettler@[EMAIL PROTEC  2004-07-26 21:58:33 
Re: Serious question on using Perl or not...
nferraz@[EMAIL PROTECTED]  2004-07-26 17:47:40 
Re: Serious question on using Perl or not...
hursh@[EMAIL PROTECTED]   2004-07-26 16:25:19 
Re: Serious question on using Perl or not...
uri@[EMAIL PROTECTED] (U  2004-07-27 01:22:38 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Jul 24 12:58:53 CDT 2008.