October 21, 200817 yr Hi, Is there a way to replace an element in a list by using it's index? Let say a function such as ReplaceAtIndex(list; index; theNewValue)? If not how would you work around this problem? I want to replace numerical values in a list that contain bins to create a column graph.
October 21, 200817 yr you mean a return separated list? Cat Dog Mouse you want to replace dog at value position 2 with Horse?
October 21, 200817 yr Author Thanx! It's not exactly what I was looking for, but I guest I could modify this function for my purpose. To answer the first post, sort of, but the list could contain twice the word dog. Cat Dog Horse Fish Dog I want to replace the value at index 2 for snake. ReplaceAtIndex ($myList; 2; "snake")
October 21, 200817 yr Try something like: LeftValues ( listOfValues ; index - 1 ) & replaceValue & ¶ & RightValues ( listOfValues ; ValueCount ( listOfValues ) - index ) Note: index ≤ 0 or index > ValueCount ( listOfValues ) will APPEND the new value at the beginning/end of the list.
October 21, 200817 yr Author Voilà! The only bug I'm trying to figure out is if the first value is negative, the minus sign is removed. /* -------------------------------------------------------------------------------- Returns a list with value 'value' replacing the value at index 'index' Syntax: InsertValue ( valueList ; value ; index ) In: valueList - the return delimited list to search value - the stringvalue to insert index - the position (1-...) where the new value should go If left empty or an integer value greater than the total size of the list, the new value will be added at the very end of the list. If <= 1 the value will added at the beginning of the list. Return type: Text (a list with leading and trailing returns removed) ReplaceAtIndex ( "Lemieux¶Gretzky¶Robitaille" ; "Messier" ; 2 ) --> "Lemieux¶Messier¶Robitaille" Nicolas Bourré Based on Theo Ros script -------------------------------------------------------------------------------- */ Let ( [ value = LeftWords( value; 999999999 ) ; list = LeftWords ( valueList; 999999999 ) ; totalvalues = ValueCount ( list ) ] ; Case ( totalvalues = 0 or IsEmpty ( index ) or index > totalvalues; value ; index < 2 ; value & "¶" & MiddleValues(list; index + 1; 999999999); LeftWords ( LeftValues ( list ; index - 1 ) & value & "¶" & MiddleValues ( list ; index + 1 ; 999999999 ) ; 999999999 ) ) )
October 22, 200817 yr I don't like the 99999 thing. I'd suggest this. // SetValue ( ListOfValues; replaceValue; index) Let( [ LVC = ValueCount( ListOfValues); Diff = LVC - Index ]; LeftValues ( listOfValues ; index - 1 ) & Case( Diff > 0; LeftValues( replaceValue; 1) & RightValues ( listOfValues ; Diff - 1 ) & GetValue( ListOfValues; LVC) ; GetValue( ReplaceValue; 1) ) )
October 22, 200817 yr Author Yeah I don't like it either but I didn't had time to retouch the calculation. Thanx for the tip.
October 22, 200817 yr Updated, there are a few problems with the original. Here, if you use index of 0, item is appended to top of list, if index > list value count, item is appended to end of list. If you use negative values, e.g. -1 or -2, item is positioned counting back from end of list. // SetValue ( ListOfValues; replaceValue; index) Let( [ LVC = ValueCount( ListOfValues); Index = Case( Index > -1 ; index; Abs( Index) > LVC ; 0; LVC + 1 + index ); Diff = LVC - Index ]; LeftValues ( listOfValues ; index - 1 ) & Case( Diff > 0; LeftValues( replaceValue; 1) & MiddleValues ( listOfValues ; Index + 1 ; Diff - 1 ) & GetValue( ListOfValues; LVC) ; GetValue( ReplaceValue; 1) ) ) Edited October 22, 200817 yr by Guest
Create an account or sign in to comment