Question about arrays

Bijan Parsia bparsia at email.unc.edu
Tue Nov 20 00:02:33 UTC 2001


On Mon, 19 Nov 2001, Christopher K Roach wrote:

> ok, I have a RECORD with the some elements in it, does anyone know how to
> make an array of records?

Yes. But I think you are in *dire* need of a Smalltalk primer.

| foo |
foo := Array new: 100.

foo at: 1 put: Record new.
...

Better would be to use an OrderedCollection, which can work like a stack.

| foo |

foo := OrderedCollection new.
foo add: Record new.

-----

> Object subclass: #Record
> 
> Can I do :
> 
> Array subclass: #Record

Uh.. You can. But that makes Record a *kind* of array.

> I think you have to made record a subclass of an array?

Why? You just want to put stuff *into* arrays.

>  however I dont know
> how to reference the array is you know what I mean?  If i know the maximum
> array to be 10,000 I could create 10,000 array BUT that is soo inefficient !
> ! ! Dynamic arrays...

See OrderedCollections, or Streams on arrays.
 
[snip]

I don't know why you're using "globals" (i.e., stuffing stuff into
Smalltalk), but that doesn't seem like a good idea. Look up "temps" or
"temporary variables" and "workspaces".

> this does not work after me working my way through the glourious tutorial of
> smalltalk.  But I still have a problem of storing since where do I store
> them to if I dont have an array?

Well, you could have stored them in Smalltalk :) But I don't think that's
a good idea, personally.

Look up "inspectors" too.

>  so they are both conencted problems.
> Above SHOULD work since it well bloody well makes sense does it not?  if a
> is:
> 
> Smalltalk at: #a put: (Record new) !

I'm not following you here.

> I even made a print method to display to my screen the contents of the
> record:
[snip]
> but all smalltalk does is display:
> 
> element1 is:  BLANK ... etc.

Because Smalltalk at: #a is a blank record?

> so that means its NOT reading from MY file.  ummm help please.

(copy and paste this into a workspace):

| results stream |
 
results := OrderedCollection new.
stream := ReadStream on: 'foo|bar|baz|bat|'.
[stream atEnd] whileFalse: 
	[results add: (stream upTo: $|)].
results inspect.

"Printing" this will pop up an inspector *and* insert:

	an OrderedCollection('foo' 'bar' 'baz' 'bat')

Into the workspace.

This seems to be the basic structure you're working with, modulo various
polymorphic subsitutions (e.g., FileStream for ReadStream)

It seems to me that your question is more about Objects and variables,
than arrays per se :) Remember that Arrays are just objects.

Cheers,
Bijan Parsia.






More information about the Squeak-dev mailing list