Find structure in array

View: New views
5 Messages — Rating Filter:   Alert me  

Find structure in array

by Carl Vanderpal :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Having a stumpt moment, how to I find what position in the array 'DEF' is in?

Thanks
<cfscript>

aBooks = ArrayNew(1);

stBooks[1] = StructNew();
stBooks[1].Name = "ABC";
stBooks[1].Chapters = 50;

aBooks[2] = ArrayNew(1);
stBooks[2] = StructNew();
stBooks[2].Name = "DEF";
stBooks[2].Chapters = 40;

aBooks = ArrayNew(1);
stBooks[3] = StructNew();
stBooks[3].Name = "HIJ";
stBooks[3].Chapters = 27;

aBooks[4] = ArrayNew(1);
stBooks[4] = StructNew();
stBooks[4].Name = "KLM";
stBooks[4].Chapters = 36;
</cfscript>


====================================
Postal: Po Box 3462 Dural, NSW 2158
Email: mailto:carl.vanderpal@...
FireFly Internet Phone: 80011777 --- You are currently subscribed to cfaussie as: lists@... To unsubscribe send a blank email to leave-cfaussie-23165F@... Aussie Macromedia Developers: http://lists.daemon.com.au/

Parent Message unknown Re: Find structure in array

by Ben Lowndes :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

Your sample code doesn't produce a meaningful array, but I can see
what you're trying to do.

The simplest way would be to cfloop over the array looking at each
name element, e.g:

<cfloop from="1" to="#arrayLen(aBooks)#" index="iBook">
   <cfif aBooks[iBook].name EQ "DEF">
        <cfset bookDEF = iBook />
   </cfif>
</cfloop>

Or you could use one of CF's built in functions, look at structFind()
or structFindValue() as I believe these will work on arrays as well as
structs.

Deciding which to use may depend on performance, so I'd test both
techniques if the array is large.

Ben

On 12/28/05, Carl Vanderpal <carl.vanderpal@...> wrote:

> Having a stumpt moment, how to I find what position in the array 'DEF' is in?
>
>  Thanks
>  <cfscript>
>
>  aBooks = ArrayNew(1);
>
>  stBooks[1] = StructNew();
>  stBooks[1].Name = "ABC";
>  stBooks[1].Chapters = 50;
>
>  aBooks[2] = ArrayNew(1);
>  stBooks[2] = StructNew();
>  stBooks[2].Name = "DEF";
>  stBooks[2].Chapters = 40;
>
>  aBooks = ArrayNew(1);
>  stBooks[3] = StructNew();
>  stBooks[3].Name = "HIJ";
>  stBooks[3].Chapters = 27;
>
>  aBooks[4] = ArrayNew(1);
>  stBooks[4] = StructNew();
>  stBooks[4].Name = "KLM";
>  stBooks[4].Chapters = 36;
> </cfscript>
>
>
> ====================================
> Postal: Po Box 3462 Dural, NSW 2158
> Email: mailto:carl.vanderpal@...
> FireFly Internet Phone: 80011777   --- You are currently subscribed to cfaussie as: ben.lowndes@... To unsubscribe send a blank email to leave-cfaussie-8745J@...  Aussie Macromedia Developers: http://lists.daemon.com.au/

---
You are currently subscribed to cfaussie as: lists@...
To unsubscribe send a blank email to leave-cfaussie-23165F@...
Aussie Macromedia Developers: http://lists.daemon.com.au/

Parent Message unknown RE: Find structure in array

by Taco Fleur - Pacific Fox :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Loop over the array and find the position, i.e.
 
found = 0;
for ( i = 1; i LTE arrayLen( stBooks ); i = i + 1 )
{
    if ( stBooks[ i ] IS 'DEF' ) // Or use compare() if you are after a case match
    {
        found = i;
        break;
    }
}
 
Kind regards,
 

Taco Fleur - Chief Executive Officer
Pacific Fox http://www.pacificfox.com.au an industry leader with commercial IT experience since 1994 …

  • Web Design and Development
  • SMS Solutions, including developer API
  • Domain Registration, .COM for as low as fifteen dollars a year, .COM.AU for fifty dollars two years!
  • BlackBerry® Business Solutions www.OzBlackBerry.com
  • We endorse PayPal, accept payments online now!
  • Seamless Merchant integration
 


From: bounce-cfaussie-21343@... [mailto:bounce-cfaussie-21343@...] On Behalf Of Carl Vanderpal
Sent: Wednesday, 28 December 2005 4:59 PM
To: CFAussie Mailing List
Subject: [cfaussie] Find structure in array

Having a stumpt moment, how to I find what position in the array 'DEF' is in?

Thanks
<cfscript>

aBooks = ArrayNew(1);

stBooks[1] = StructNew();
stBooks[1].Name = "ABC";
stBooks[1].Chapters = 50;

aBooks[2] = ArrayNew(1);
stBooks[2] = StructNew();
stBooks[2].Name = "DEF";
stBooks[2].Chapters = 40;

aBooks = ArrayNew(1);
stBooks[3] = StructNew();
stBooks[3].Name = "HIJ";
stBooks[3].Chapters = 27;

aBooks[4] = ArrayNew(1);
stBooks[4] = StructNew();
stBooks[4].Name = "KLM";
stBooks[4].Chapters = 36;
</cfscript>


====================================
Postal: Po Box 3462 Dural, NSW 2158
Email: mailto:carl.vanderpal@...
FireFly Internet Phone: 80011777 --- You are currently subscribed to cfaussie as: taco.fleur@... To unsubscribe send a blank email to leave-cfaussie-23165F@... Aussie Macromedia Developers: http://lists.daemon.com.au/
---
You are currently subscribed to cfaussie as: lists@...
To unsubscribe send a blank email to leave-cfaussie-23165F@... Aussie Macromedia Developers: http://lists.daemon.com.au/

Parent Message unknown RE: Find structure in array

by Carl Vanderpal :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



On 12/29/05, Taco Fleur - Pacific Fox <taco.fleur@...> wrote:
Loop over the array and find the position, i.e.
 
found = 0;
for ( i = 1; i LTE arrayLen( stBooks ); i = i + 1 )
{
    if ( stBooks[ i ] IS 'DEF' ) // Or use compare() if you are after a case match
    {
        found = i;
        break;
    }
}


Thanks Taco...& Ben


====================================
Postal: Po Box 3462 Dural, NSW 2158
Email: mailto:carl.vanderpal@...
FireFly Internet Phone: 80011777 --- You are currently subscribed to cfaussie as: lists@... To unsubscribe send a blank email to leave-cfaussie-23165F@... Aussie Macromedia Developers: http://lists.daemon.com.au/

Parent Message unknown RE: Find structure in array

by Dawes, Chris :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

use eq instead of is... much quicker.

Cheers,

Chris Dawes
Web Systems Administrator 
Information Technology

Tel: (02) 9030 1163
Fax: (02) 9030 1163
Mob: 0433 994 420

Level 3, Tower 1 Darling Park
201 Sussex Street
Sydney, NSW 2000

 


From: bounce-cfaussie-11844@... [mailto:bounce-cfaussie-11844@...] On Behalf Of Carl Vanderpal
Sent: Thursday, 29 December 2005 6:32 PM
To: CFAussie Mailing List
Subject: [cfaussie] RE: Find structure in array



On 12/29/05, Taco Fleur - Pacific Fox <taco.fleur@...> wrote:
Loop over the array and find the position, i.e.
 
found = 0;
for ( i = 1; i LTE arrayLen( stBooks ); i = i + 1 )
{
    if ( stBooks[ i ] IS 'DEF' ) // Or use compare() if you are after a case match
    {
        found = i;
        break;
    }
}


Thanks Taco...& Ben


====================================
Postal: Po Box 3462 Dural, NSW 2158
Email: mailto:carl.vanderpal@...
FireFly Internet Phone: 80011777 --- You are currently subscribed to cfaussie as: chris_dawes@... To unsubscribe send a blank email to leave-cfaussie-23165F@... Aussie Macromedia Developers: http://lists.daemon.com.au/
---
You are currently subscribed to cfaussie as: lists@...
To unsubscribe send a blank email to leave-cfaussie-23165F@... ---
You are currently subscribed to cfaussie as: lists@...
To unsubscribe send a blank email to leave-cfaussie-23165F@... Aussie Macromedia Developers: http://lists.daemon.com.au/ Aussie Macromedia Developers: http://lists.daemon.com.au/
----------------------------------------------------
This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify the system manager. Please note that any views or opinions presented in this email are solely those of the author and do not necessarily represent those of the company. The recipient should check this email and any attachments for the presence of viruses. The company accepts no liability for any damage caused by any virus transmitted by this email.
----------------------------------------------------


commander.gif (4K) Download Attachment