Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

working with lists & loops

Status
Not open for further replies.

Leakyradiator

Technical User
Jul 12, 2002
35
0
0
US
I have a list with the following items in it:

147,101,102,145,134,135,147117,134109

Here's what I want to do:
if the first 3 numbers of the 6 digit items equals any of the three digit numbers, (they always will,) I want to prepend to the 6 digit number a 3 digit number (arbitrarily, 555) and delete the three digit number that matches the first 3 digits of the 6 digit number.

for example: the lists shows 147117. I want the final output to be 555147117. And because 147117 starts with 147, I want to delete the 3 digit 147 from the list.
the after all the corresponding 3 digit numbers are deleted, I want to prepend the remaining 3 digit numbrers with 555 and append the number 999 to each of the 3 digit numbers

the final list would look like 555147117,555134109,555101999,555102999,555145999,555135999

Thanks
 
I think this should do it

Code:
<cfset pt="">
<cfset px="">
<cfset plist="147,101,102,145,134,135,147117,134109">
<cfloop list="#plist#" index="p">
  <cfif not listfind(px,p)>
    <cfif len(p) eq 6>
      <cfset pn = "555" & p>
      <cfset pt = listappend(pt,pn)>
      <cfset px = listappend(px,left(p,3))>
    <cfelse>
      <cfset pn = "555" & p & "999">
      <cfset pt = listappend(pt,pn)>
    </cfif>
  </cfif>
</cfloop>

<cfoutput>Your old list was #plist#.
Your new list is "#pt#"</cfoutput>

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
I greatly appreciate the help. Here's what I ended up with:
(this is
Your old list was 147,101,102,145,134,135,147117,145114,134109.
Your new list is "555147999,555101999,555102999,555145999,555134999,555135999,555135999,555135999,555135999"

As you can see, the 6 digit numbers just repeat with the 135 in the middle.

What I'm trying to achieve is this outcome:
Your old list was 147,101,102,145,134,135,147117,145114,134109.
Your new list is:
555147117,555101999,555102999,555145114,555134109,555135999

since the first item in the old list (147) is the same as the first 3 digits of the 7th item, the first item should disappear and the 7th item should become 555147. The second item (101) has no matching items later in the list and its output should be 555101999.

I can break the first list into 2 separate lists (one three digit list and one 6 digit list) if that makes more sense.

 
This might make more sense:

I have two lists:
List 1 List 2
147 147117
101 145114
102 134109
145
134
135

I want to step through list 1. If the first 3 digits of any item in list 2 equal the item in list 1, I want to add 555 to the beginning of the item in list 2 and output it (preferably into a 3rd list). The output would be 555145114.
On the second item in list 1, there is no corresponding 3 digit prefix in list 2, so I want to add 555 at the biginning and 999 at the end and output 555102999.
The 3rd item would be 555102999
The output for the 4th item would be 555145114.

Thanks again for your help
 
Code:
<cfset pt="">
<cfset px="">
<cfset plist="147,101,102,145,134,135,147117,134109">
<cfloop list="#plist#" index="p">
  <cfif not listfind(px,p)>
    <cfif len(p) eq 6>
      <cfset pn = "555" & p>
      <cfset pt = listappend(pt,pn)>
      <cfset px = listappend(px,left(p,3))>
    <cfelseif len(p) eq 3>
      <cfset pn = "555" & p & "999">
      <cfset pt = listappend(pt,pn)>
    </cfif>
  </cfif>
</cfloop>

<cfoutput>Your old list was #plist#.<br>
Your new list is "#pt#"<br><br>

This works for me, try that.

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
Erg.. change..

Code:
<cfoutput>Your old list was #plist#.<br>
Your new list is "#pt#"<br><br>

to

Code:
<cfoutput>Your old list was #plist#.<br>
Your new list is "#pt#"<br><br>[red]</cfoutput>[/red]

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
I'm curious, if you don't mind, to know if this worked... I know I get an output from it that I think works but its really up to you.

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
Your code changed each item in the category list to the 555[list#]999 that I was looking for. What it didn't do, was delete the 3 digit category number if that number was the first 3 digits of the 6 digit number appearing later in the list. I greatly appreciate your input, though, because it did get me thinking about the problem in a little different way so that I could come up with a solution.

In the end, I sent the data from a form in two lists. One clist (3 digit #'s) and slist (6 digit #'s). I then combined the lists as plist and did a comparison of plist against clist and deleted the 3 number matches.

Here's the code I ended up with:

<!---combine c list and s list--->
<cfset plist=listappend(clist,slist)>
<!---delete c where 1st 3 digits = 1st 3 digits of s--->
<cfloop list="#clist#" index="cat">
<cfloop list="#slist#" index = "sub">
<!---identify and delete duplicate 3 digit #'s--->
<cfif #left(sub,3)# eq #cat#>
<cfset delcat=listfind(clist,#cat#)>
<cfset clist=listdeleteat(clist,#delcat#)>
</cfif>
</cfloop>
</cfloop>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top