Kindly help to solve!

Hi

 I have struct and array values to be compared dynamically and if value matches - remove the matched values from the array. I have tried a lot, But error occurs - values match!! in if loop condition but always only else part works and displays results as values not match!! kindly please help, my code is:

///// function to compare array and struct values - deletes the matched value and returns the array:

function addr_Check(arr, st_addr){
   /// dump(arr);
    echo("<br>");
  //  dump(st_addr);
temp = arrayNew(1);

// if(structCount(st_addr) != 0){
if(!structIsEmpty(st_addr)){
st_addr_count = structCount(st_addr);
echo(st_addr_count);

      for(i=1; i<= st_addr_count; i++){
        echo("i count: "& i);
        for(it=1; it<= cnt_N; it++){
          echo("it:" & it); 
          /*
          echo("<br>");
          dump(st_addr[itm][it].CITY);
          echo("<br>");
          dump(st_addr[itm][it].STATE);
          echo("<br>");
          dump(st_addr[itm][it].STREET);
          echo("<br>");
          dump(st_addr[itm][it].ZIP);
          echo("<br>");  */

          for(var j=1; j <= arrayLen(arr); j++){

              for(var K=1;k <= arrayLen(arr[j]); k++){ 
                echo("arr[j][k]: <br>");
                dump(arr[j][k]); 
                  echo("arr[j][k]: <br>");
                dump(st_addr[itm][it].street); 
                echo("<br>");
               // abort;   
                if(arr[j][k] == st_addr[itm][it].street && arr[j][k] == st_addr[itm][it].state && arr[j][k] == st_addr[itm][it].zip){

                        writeOutput("Values Matched!!");   
                       // temp[j] = j;
                       // echo(temp);
                       ArrayDeleteAt(arr, j);
                    
                  } else{
                    writeOutput("Not Match!");
                  } 
                
                } 
            } 

        }
      }

} else{
    echo("No Address to skip.");
}

return arr;

} //function end

addr_Check(inputArr,st);
echo(“After removing the matched values”);
dump(inputArr);

Hi @Shalini and welcome!

Are you new to CFML? What is what you want to do? I’ve seen that you have created array with addresses, but I really don’t know what you are wanting to do. Could you please explain in words, what is what you want to do? Do you just want to loop through all those addresses and output it to the browser/screen? Please explain in words.

From what I understand… is this what you want to do?

<cfscript>
	public array function getAddresses() {

	inputAddress = [

		{
			street: "1420 Washington",
			state: "NY",
			zip: "10456"
		},
		{
			street: "7100 TUCKER RD",
			state: "NC",
			zip: "27052"
		},
		{
			street: "232 Homer Park Dr Watertown",
			state: "NY",
			zip: "13601"
		},
		{
			street: "11, Broadway",
			state: "NY",
			zip: "10004"
		},

	];

	return inputAddress;

}

public array function removeAddressByStreet(addresses, street) {

	result = addresses.reduce(function(acc, element, index) {
		if (element.street != street) {
			acc.append(element);
		}
		return acc;
	}, []);

	return result;

}

streetToRemove = "232 Homer Park Dr Watertown";
allAdresses = getAddresses();
filteredAddresses = removeAddressByStreet(allAdresses, streetToRemove);

dump(filteredAddresses);

</cfscript>
<cfoutput>
<cfloop array="#filteredAddresses#" item="address">
	#encodeForHTML( address.street )#<br>
	#encodeForHTML( address.state )#<br>
	#encodeForHTML( address.zip )#<br>
</cfloop>
</cfoutput>
1 Like

Hi @andreas, Thanks for coming up!

YES, I am new to CFML,

  • I have list of addresses in struct and list of addresses in array(2D Array).
  • both struct and array passed as arguments in function.
  • Inside function must compare the both addresses list.
  • If address in struct matches with address in array, then the matched address from the array must be removed and return the same array (Array must be returned by removing all matched address.)

For example,
we have 20 addresses in Struct and 500 addresses in 2D - Array,(Then, Array has 500 rows, Each row consists of 25 Columns - Have to compare each column value with Struct value to find matching value).

  • If 15 addresses in Struct matches with Array list addresses, then matched 15 addresses must be removed from the array and return array (now array must have row of 485 addresses), which is done Dynamically.

kindly please help to find the solution. This is my code:
2D - Array:

<cfset inputAddress[1][i] = “” />

<cfset inputAddress[1][7] = “1420 Washington” />

<cfset inputAddress[1][10] = “NY” />

<cfset inputAddress[1][11] = “10456” />

<cfset inputAddress[2][i] = “” />

<cfset inputAddress[2][7] = “7100 TUCKER RD” />

<cfset inputAddress[2][10] = “NC” />

<cfset inputAddress[2][11] = “27052” />

<cfset inputAddress[3][i] = “” />

<cfset inputAddress[3][7] = “232 Homer Park Dr Watertown” />

<cfset inputAddress[3][10] = “NY” />

<cfset inputAddress[3][11] = “13601” />

<cfset inputAddress[4][i] = “” />

<cfset inputAddress[4][7] = “11, Broadway” />

<cfset inputAddress[4][10] = “NY” />

<cfset inputAddress[4][11] = “10004” />

////List of Address in Struct:

// struct Skip Address st_skip_Addr = { NY: [ { "CITY": "Watertown", "STATE": "NY", "STREET": "232 Homer Park Dr", "ZIP": "13601" }, { "CITY": "NEW YORK", "STATE": "NY", "STREET": "11, Broadway", "ZIP": "10004" }, { "CITY": "Brooklyn", "STATE": "NY", "STREET": "2081 E 8th St", "ZIP": "11223" } ] };

///function:

function addr_Check(arr, st_addr){
   /// dump(arr);
    echo("<br>");
  //  dump(st_addr);
temp = arrayNew(1);

// if(structCount(st_addr) != 0){
if(!structIsEmpty(st_addr)){
st_addr_count = structCount(st_addr);
echo(st_addr_count);

      for(i=1; i<= st_addr_count; i++){
        echo("i count: "& i);
        for(it=1; it<= cnt_N; it++){
          echo("it:" & it); 
          /*
          echo("<br>");
          dump(st_addr[itm][it].CITY);
          echo("<br>");
          dump(st_addr[itm][it].STATE);
          echo("<br>");
          dump(st_addr[itm][it].STREET);
          echo("<br>");
          dump(st_addr[itm][it].ZIP);
          echo("<br>");  */

          for(var j=1; j <= arrayLen(arr); j++){

              for(var K=1;k <= arrayLen(arr[j]); k++){ 
                echo("arr[j][k]: <br>");
                dump(arr[j][k]); 
                  echo("arr[j][k]: <br>");
                dump(st_addr[itm][it].street); 
                echo("<br>");
               // abort;   
                if(arr[j][7] == st_addr[itm][it].street && arr[j][10] == st_addr[itm][it].state && arr[j][11] == st_addr[itm][it].zip){

                        writeOutput("Address Matched!!");   
                       // temp[j] = j;
                       // echo(temp);
                       ArrayDeleteAt(arr,j);
                    
                  } else{
                    writeOutput("Not Match!");
                  } 
                
                } 
            } 

        }
      }

} else{
echo(“No Address to skip.”);
}

return arr;

} //function end

addr_Check(inputAddress,st_skip_Addr);

///Struct Loop:

index_count: #cnt_idx#


  </cfloop>

I have sloved with static values, wanted to work with Dynamic values:

This is solved code with static values:


<cfset inputAddress[1][i] = “” />

<cfset inputAddress[1][7] = “1420 Washington” />
<cfset inputAddress[1][10] = “NY” />
<cfset inputAddress[1][11] = “10456” />

<cfset inputAddress[2][i] = “” />

<cfset inputAddress[2][7] = “7100 TUCKER RD” />
<cfset inputAddress[2][10] = “NC” />
<cfset inputAddress[2][11] = “27052” />

<cfset inputAddress[3][i] = “” />

<cfset inputAddress[3][7] = “232 Homer Park Dr Watertown” />
<cfset inputAddress[3][10] = “NY” />
<cfset inputAddress[3][11] = “13601” />

// struct Skip Address st_skip_Addr = { NY: [ { "CITY": "Watertown", "STATE": "NY", "STREET": "232 Homer Park Dr", "ZIP": "13601" }, { "CITY": "NEW YORK", "STATE": "NY", "STREET": "11, Broadway", "ZIP": "10004" }, { "CITY": "Brooklyn", "STATE": "NY", "STREET": "2081 E 8th St", "ZIP": "11223" } ] };
function addr_Check(arr, st_addr){
if(!structIsEmpty(st_addr)){
  //  echo(structCount(st_addr));   

  
      if(arr[4][7] == st_addr[itm][2].street && arr[4][10] == st_addr[itm][2].state && arr[4][11] == st_addr[itm][2].zip){
            echo("<br>");
            writeOutput("<h3 style='color:red'>"&st_addr[itm][2].street &"  "& st_addr[itm][2].state &"  "&st_addr[itm][2].zip &"  Address Matched!</h3>");
            arrayDeleteAt(arr, 4);
            
          } else{
            echo("Address not Match!!");
          } 

} else{
    echo("Skip  Address List not available!");
}

return arr;

} //function end

writeOutput(“

Vendor Address Array before removing skip address, Array length: “&arrayLen(inputAddress) & “

”);
dump(inputAddress);
echo(”
”);
addr_Check(inputAddress,st_skip_Addr);
echo(“
”);
writeOutput("

Vendor Address Array after removing skip address, Array length: "&arrayLen(inputAddress) & “

”);
dump(inputAddress);

1 Like

Really, really welcome!!! Have you seen my example above? That’s how I’d structure the data and try the tasks. I’ve seen that you have saved all the data as pure arrays at the beginning. That would make thinks much more complicated. If the reduce function feels a little complicated (beginners usually don’t have such a big issue with such function - higher-order-functions - let us know). I’ve created an array of struct. Take a look above:

AS a start in CFML, there is not much about modern CFML content around, but if you into JavaScript you will feel pretty comfortable soon. The best you can get at the moment is:
https://learncfinaweek.com/course/index.html

@andreas Thanks for the course Link, I will do it. I have Struct inside array inside Struct, as struct format in my code.

Thanks for the code, Your code is more understandable to learn, Thanks! Thanks!

1 Like

If you have such questions and you are struggling going a step further and not finding a way by googling how to achieve it, please come back and post it here. Modern CFML is a very powerfull language and really easy to learn (great learning curve), its just that there is not much content about it around.

2 Likes

@Shalini

I’ve just reminded that there’s an even simpler way to “filter” by street name. Don’t know why I haven’t used at the the first time and went with the reduce() function. filter() is way simpler for this purpose. Just replace the function removeAddressByStreet() above with the following new function and you will understand even more!

public array function removeAddressByStreet(addresses, streetfilter) {

	result = addresses.filter( function( singleAddress ) {
		return singleAddress.street!=streetfilter;
	});

	return result;

}
1 Like