looping over a list problem

Ask a Question related to Coldfusion - Advanced Techniques, Design and Development.

  1. #1

    Default looping over a list problem

    I am having a problem looping over a list with some elements that are empty.
    Here is a sample of the list: address_street=8349+White+Dr custom=
    address_state=FL address_city=Burnsville address_id= first_name=Evan
    last_name=Whats As you can see some of the items do not = any thing. I have no
    control over this, it is what I get. Here is the code I am using. Basicly I
    want to end up with a variable = data so I can use them in the page. <cfloop
    index='curLine' list='#datalist#' delimiters='#chr(10)#'> <cfset name =
    listGetAt(curLine,1,'=')> <cfset '#name#' = listGetAt(curLine,2,'=')> </cfif>
    </cfloop> This is the error I get Invalid list index 2. In function
    ListGetAt(list, index [, delimiters]), the value of index, 2, is not a valid as
    the first argument (this list has 1 elements). Valid indexes are in the range 1
    through the number of elements in the list. The error occurred in line 33
    31 : <cfloop index='curLine' list='#datalist#' delimiters='#chr(10)#'> 32 :
    <cfset name = listGetAt(curLine,1,'=')> 33 : <cfset '#name#' =
    listGetAt(curLine,2,'=')> 34 : </cfloop> 35 : I know it is because some of
    the items in the list do not = anything. I would like to just test first to see
    if they don't = any thing and skipp them but as of yet everything I try makes
    more errors. Any help would be great thanks Jason

    jmercmon Guest

  2. Similar Questions and Discussions

    1. Looping a list in SQL 2000 SPROC
      Hey guys, just trying to learn something new today and wanted to convert one of my cf pages to a sproc. This page just sets up security for the...
    2. Problem Looping
      I am trying to create an array of structures using data collected from a form. The form contains 5 sets of seven fields each set containing...
    3. parsing and looping over a list
      All, I need to parse a list that looks like this: de={ts '2004-08-06 12:14:00'}|s=title header|entID=15,de={ts '2004-08-06 13:05:00'}|s=title...
    4. [PHP] Looping through a list - Newbie question
      There are a lot of methods. The most common is using an array: $_SESSION = Array ("1","2","3","4","5"); foreach($_SESSION as $id) { echo $id; }...
    5. Looping through a list - Newbie question
      explode() and then foreach are your friends :) explode() using the comma as your separator and then traverse the array using foreach. ...
  3. #2

    Default Re: looping over a list problem

    try replacing all =chr(10) with =(space)chr(10) so before anything treat
    your list like a simple string... <cfset mylist = replace('='&amp;chr(10),'=
    '&amp;chr(10),'ALL')> then deal with the new list... if that doesn't work with
    the space replace it with '=@#$%'&amp;chr(10) the likely hood that the string
    combo @#$% will occur naturally in your list is slim to none so then on output
    you can replace @#$% with a space... it should work with the '= ' however
    then you just need to trim() on output... Ultra

    ultrafresh Guest

  4. #3

    Default Re: looping over a list problem

    oh my now thats all messed up i will retype it... try replacing all =chr(10)
    with =(space)chr(10) so before anything treat your list like a simple
    string... <cfset mylist = replace(mylist,'='&amp;chr(10),'=
    '&amp;chr(10),'ALL')> then deal with the new list... if that doesn't work with
    the space replace it with '=@#$%'&amp;chr(10) the likely hood that the string
    combo @#$% will occur naturally in your list is slim to none so then on output
    you can replace @#$% with a space... it should work with the '= ' however
    then you just need to trim() on output... Ultra

    ultrafresh Guest

  5. #4

    Default Re: looping over a list problem

    Not really having a problem with the = if I do something like this: <cfloop
    index='curLine' list='#cfhttp.FileContent#' delimiters='#chr(10)#'> <cfif
    listGetAt(curLine,1,'=') is 'first_name'> <cfset
    first_name=listGetAt(curLine,2,'=')> </cfif> </cfloop> I can get the info I
    want but the lines that jsut say something= and then nothing will
    not work with this either. So not matter if I list them all out like above or
    in the first post I have to find some way to skip the ones that are 'something=
    '. Like this cfloop index='curLine' list='#datalist#'
    delimiters='#chr(10)#'> <cfif SOME CODE TO MAKE IT SKIP IF THE SECOND
    ELEMENT IS NOT THERE> <cfset name = listGetAt(curLine,1,'=')> <cfset '#name#' =
    listGetAt(curLine,2,'=')> </cfif> </cfloop> I have tryed IsDefined and I get a
    really wacky error: Parameter 1 of function IsDefined, which is now
    '8349+White+Dr', must be a syntactically valid variable name. Just not sure
    what little bit of code I need. I have tried many things but nothing works. I
    am game to try anything. Jason

    jmercmon Guest

  6. #5

    Default Re: looping over a list problem

    Here's how I handle empty elements in a list: <cfset
    Count=#ListLen(ConditionID)#> <cfloop index='LoopIndex' from='1' to=#Count#>
    <CFIF #Evaluate('ParameterExists(ConditionExists#LoopInd ex#)')#> <cfset
    ConditionExistsElement = listgetat(#ConditionID#,#LoopIndex#)> <cfelse> <CFSET
    ConditionExistsElement = ' '> </cfif> So for your code I would use: <cfloop
    index='curLine' list='#datalist#' delimiters='#chr(10)#'> <cfset name =
    Evaluate('ParameterExists(listGetAt(curLine,1,'=') ))> <cfset '#name#' =
    Evaluate('ParameterExists(listGetAt(listGetAt(curL ine,2,'=')))> </cfif>
    </cfloop> Hopefully this helps

    MikelBGI Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139