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

Help with Structure in array

Community Beginner ,
Jun 04, 2009 Jun 04, 2009

I need to process a tab delimited list and I'm able to put the data into an array. See the image of the array below:

arr.JPG

Now that the list is separated into 5 array elements, each element is a list by itself.

How can I create a structure for the list within each array element so I will have an Array with 5 element and each element contain a structure with keys and values???

To create the above array I simply use :

<cffile action="READ" file="#FeedPath##FileName#" variable="MyFile">

<cfset MyArray = listtoarray(MyFile,"#chr(13)##chr(10)#")>

Please help!

TOPICS
Getting started
1.4K
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 ,
Jun 04, 2009 Jun 04, 2009

Well the basis for doing what you want is to loop over each record that builds the array and create a structure out of it.

How you do that is going to very somewhat depending on if you want to do this directly from the file so that in one larger fill reading step, you end up with and array of structures.  Or if you want to do it after you have read the file and created the array like you have already done, this would.

The latter is a bit simplier to show, but takes a tiny bit longer to process since you are effectivily looping over all the data twice.

<cfset myArray2 = arrayNew(1).

<cfloop array="#myArray#" index="i">

  <cfset arrayAppend(myArray2, structNew())>

  <cfset myArray2["key1"] = somePartOf(myArray)>

  <cfset myArray2["key2"] = somePartOf(myArray)>

  <cfset myArray2["key3"] = somePartOf(myArray)>

</cfloop>

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
Community Beginner ,
Jun 04, 2009 Jun 04, 2009

Ian,

one of my problem is how can I set the keys and values set from the list within each array element?

This is not a fixed length so how can I determined which key for which value?

for example with the list within the first element is 0035115752 H 42041 Orange Drive. Temecula CA 92591 UNG 20081124

how can I put the value of 0035115752 as id, H as addressType, 42041 Orange Drive. Temecula as StreetName, etc?

For fixed length it is easier since I know exactly the position of each item but this is a tab delimited.

So I guess in you example, I don't quite undertand what should I do with the

somePartOf(MyArray

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 ,
Jun 04, 2009 Jun 04, 2009

alecken wrote:


So I guess in you example, I don't quite undertand what should I do with the

somePartOf(MyArray

Well "somePartOf(myArray)" was vague, because I had no idea how you wanted to parse up your data into a structure.  The previous post gives some idea how you want to parse it up, but there are still chalanges you will have to work out because it is your data.  You could easily treat the value as a space delimited list, but you do not seem to have a one to one relationship between space separated values and your desired structure keys.  You will have to work out the business rules that turns that data into the structure you desire.  There is no magic "Make My Random Data into a Structure" function.

These would apply to the first two keys you described, but I have no idea how the rest of the data parses out.

<cfset myArray2["id"] = listFirst(myArray," ")>

<cfset myArray2["addressType"] = listGetAt(2,myArray," ")>

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
Community Beginner ,
Jun 04, 2009 Jun 04, 2009

Ian,

I tried to run your code just using the 2 keys/values example you wrote and I got an error (see below):

err.JPG

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 ,
Jun 04, 2009 Jun 04, 2009

Because ColdFusion does not know how to turn that long string into a number, a pretty clear indicaiton that I got my parameters for the listGetAt() funciton in the wrong order.  A quick look up in the CF online documentation shows that to be true.  So try this.

<cfset myArray2["id"] = listFirst(myArray," ")>
<cfset myArray2["addressType"] = listGetAt(myArray,2," ")>
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
Community Beginner ,
Jun 04, 2009 Jun 04, 2009

Ian, I got the exact same error.

The value 0035115752 H 42041 Orange Drive. Temecula CA 92591 UNG 20081124 cannot be converted to a number.

The error occurred in C:\Inetpub\wwwroot\scratch\DataLoad\AddressRslt.cfm: line 83
81 :                     <cfloop array="#myArray#" index="i">
82 :                    <cfset arrayAppend(myArray2, structNew())>
83 :                   <cfset myArray2["id"] = listFirst(myArray," ")>
84 :                   <cfset myArray2["addressType"] = ListGetAt(myArray,2," ")>
85 :                     </cfloop>

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 ,
Jun 04, 2009 Jun 04, 2009

What does this code do?

<cfloop array="#myArray#" index="i">

<cfoutput>#i#<br/></cfoutput>

</cfloop>

I'm guessing it shows the data from the array.  I don't use the array loop function very much yet so may be using it wrong here.

If so then this would be the older way to do this.

<cfloop from="1" to="#arrayLen(myArray)#" index="i">

....

</cfloop>

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
LEGEND ,
Jun 04, 2009 Jun 04, 2009

I find nested lists easier to deal with than 2D arrays.  To make it even easier, if you could give column headings to your text file, you can use cfhttp to create a query.

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
Community Expert ,
Jun 06, 2009 Jun 06, 2009
LATEST

<cfset myArray = listtoarray(MyFile,"#chr(13)##chr(10)#")>

<cfset myStruct = structNew()>

<cfloop from="1" to="#arrayLen(myArray)#" index="idx">
    <cfset currentRow = 'row' & idx>
    <cfset position = 1>
    <cfloop list = "#myArray[idx]#" delimiters=" " index="listElement">
        <cfset myStruct[currentRow][position] = listElement>
        <cfset position = position+1>
    </cfloop>
</cfloop>

<cfdump var="#myStruct#">

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