Joe,


I am fairly new to CF also and wanted to see if I understood the
concepts correctly.  When you get time, can you look over this and let
me know if it is correct?  I have provided two possible solutions.  The
difference in the second is that I do not use the temporary variable
thisISBN...for syntax questions.

<cfset books=new struct()>
<cfloop collection="someCollection" item="thisBook">
    <cfset thisISBN = someCollection[thisBook].isbn>
    <cfset books[thisISBN] = new struct()>
    <cfset books[thisISBN].title = someCollection[thisBook].title>
</cfloop>


OR


<cfset books=new struct()>
<cfloop collection="someCollection" item="thisBook">
    <cfset books[someCollection[thisBook].isbn] = new struct()>
    <cfset books[someCollection[thisBook].isbn] =
someCollection[thisBook].title
</cfloop>

Thank you -
  Frank

________________________________

From: Joe Rinehart [mailto:[EMAIL PROTECTED]
Sent: Tuesday, June 29, 2004 10:55 AM
To: CF-Talk
Subject: Re: first time array

Tim,

Arrays and structure are both good places to store stuff.  The
question is what type of stuff you want to store.

If you just need to store "something," an array can be a good way to
go.  If you need to store "something" and "stuff" (properties) about
it, a structure may be a good bet.

Example:  If I wanted to store the ISBN numbers of a bunch of books,
I'd use an array called "books" - books[1] would be the ISBN of the
first book, books[2] the second, and so on.

If I wanted to store a single book and "stuff" about it, I'd use a
structure - book.ISBN, book.author, book.title, etc.  Sure, you could
do this with an array: book[1] being title, book[2] being author,
etc., but then you have to remember that 1 = title, 2=author.  Think
of a structure as an array where instead of using numbers for each
element, you use a unique name.

More powerful still is an array of structures.  Going back to the
books example, you could have the books array with book[1] being a
structure, book[2] being a structure, etc., so that you have
book[1].title, book[2].title, book[3].title, and so on.

Lastly, you can have a structure of structures!   Say you developed
the app using the array of structures - then you suddenly need a
feature where you need to access the title of a book in the array by
knowing only its ISBN number.  You could create a structure called
"books", and inside put elements using their ISBN number as the name.
So you'd have books["222-442-5534"].title,
books["1232-442-5534"].title, and so on.

Whew.  If you'd like code samples for this, I can type those after
lunch. :)

________________________________
[Todays Threads] [This Message] [Subscription] [Fast Unsubscribe] [User Settings] [Donations and Support]

Reply via email to