Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Make unique array of structure

New Here ,
Aug 22, 2009 Aug 22, 2009

Hi all.

How can make array of unique elements ?

Heed to get from array -> array1

<cfset array = arrayNew(1) />
<cfset array1 = arrayNew(1) />

<cfset array[ 1 ] = { id = '1' } />
<cfset array[ 2 ] = { id = '1' } />
<cfset array[ 3 ] = { id = '1' } />
<cfset array[ 4 ] = { id = '2' } />
<cfset array[ 5 ] = { id = '3' } />
<cfset array[ 6 ] = { id = '4' } />
<cfset array[ 7 ] = { id = '4' } />
<cfset array[ 8 ] = { id = '5' } />

<cfdump var="#array#" label="Current" />

<cfset array1[ 1 ] = { id = '1' } />
<cfset array1[ 2 ] = { id = '2' } />
<cfset array1[ 3 ] = { id = '3' } />
<cfset array1[ 4 ] = { id = '4' } />
<cfset array1[ 5 ] = { id = '5' } />

<br /><br />
<cfdump var="#array1#" label="Needfull" />

1.7K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Aug 22, 2009 Aug 22, 2009

You would need to search the array each time you appended a structure. Then append the structure, only if did not already exist in the array. But that raises the question, is an array really the right type of object to be using in the case?  A structure, which does not allow duplicates, would seem more appropriate.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 22, 2009 Aug 22, 2009

if i wont delete elements if they first element eq second element etc.

<cfset limit = arrayLen(array) />
<cfloop from="#limit#" to="1" index="i" step="-1" >
    <cfloop from="1" to="#i-1#" index="j" step="1">
        <cfif array.id EQ array[j+1].id>       
            <cfset ArrayDeleteAt(array.id, j) />           
        </cfif>
    </cfloop>
</cfloop>

<cfdump var="#array#" label="Needfull" />

but i get this error:

You have attempted to dereference a scalar variable of type class java.lang.String as a structure with members.

help with syntax pls

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Aug 22, 2009 Aug 22, 2009

No. The easier way is to search the array _before_ elements are added, not after.  If for whatever reason you cannot do that, it would be easier to use a structure to eliminate the duplicates. Then convert it back to an array.

<cfset keep = {} >
<cfloop array="#array#" index="elem">
    <cfset keep[elem.id] = elem>
</cfloop>

<cfset finalArray = []>
<cfloop collection="#keep#" item="key">
    <cfset arrayAppend(finalArray, keep[key]) >
</cfloop>

<cfdump var="#finalArray#" label="finalArray" />

But, why are you using an array here?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 23, 2009 Aug 23, 2009

thx, for your reply, it works correctly, but i incorrect made a condition:

<cfset array = arrayNew(1) />
<cfset array1 = arrayNew(1) />

<cfset array[ 1 ] = { name = 'alex', priority = '1' } />
<cfset array[ 2 ] = { name = 'alex', priority = '2' } />
<cfset array[ 3 ] = { name = 'bob',  priority = '2' } />
<cfset array[ 4 ] = { name = 'alex', priority = '3' } />
<cfset array[ 5 ] = { name = 'sam',  priority = '3' } />
<cfset array[ 6 ] = { name = 'bill', priority = '3' } />
<cfset array[ 7 ] = { name = 'sam',  priority = '4' } />
<cfset array[ 8 ] = { name = 'siu',  priority = '4' } />

<cfdump var="#array#" label="current">
<hr />
<cfset array1[ 1 ] = { name = 'alex', priority = '1' } />
<cfset array1[ 2 ] = { name = 'bob',  priority = '2' } />
<cfset array1[ 3 ] = { name = 'sam',  priority = '3' } />
<cfset array1[ 4 ] = { name = 'bill', priority = '3' } />
<cfset array1[ 5 ] = { name = 'siu',  priority = '4' } />

<cfdump var="#array1#" label="expected">

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Aug 23, 2009 Aug 23, 2009

It is obvious the desired results are different, but not the reason why. We cannot read your mind You need to provide more information about:

a) what applications conditions determine "unique"

b) what code you have tried and how that code is working differently than you expect

Also, you never answered the question of why you are using an array here or why you cannot perform these checks before each element is added.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 24, 2009 Aug 24, 2009
LATEST

There are a few 'tricks' that you can use here ...

  1. A "struct" is fundamentally an associative-array structure.  So, if you've got an "id" field of any sort, a struct can locate any "id" almost instantly.  This is much faster than searching an array (which has a hidden cost created by virtual-memory paging over a large "footprint").
  2. When you add a struct to another struct or to an array, you are actually moving a reference to the structure.  You should find that (afaik!), if the struct is present both in an array and in a struct, both of these are references to one-and-the-same memory block.
  3. Use the most-convenient data structure when building the original set of records.  This is probably going to be a "struct."  If you then need to have an array of elements sorted in a particular way, build that array last, by using a "sort" operation to arrange the records in whatever order you want.  Since you'are actually (afaik...) moving references, you're not stomping on too much RAM.

"Sorting" is what I call an "unexpectedly fast and efficient" operation, because it's one of the most heavily-studied operations in all of data processing ... and has been since the days of Herman Hollerith.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources