El_Pablo Posted October 21, 2008 Posted October 21, 2008 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.
Ocean West Posted October 21, 2008 Posted October 21, 2008 you mean a return separated list? Cat Dog Mouse you want to replace dog at value position 2 with Horse?
El_Pablo Posted October 21, 2008 Author Posted October 21, 2008 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")
comment Posted October 21, 2008 Posted October 21, 2008 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.
El_Pablo Posted October 21, 2008 Author Posted October 21, 2008 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 ) ) )
bruceR Posted October 22, 2008 Posted October 22, 2008 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) ) )
El_Pablo Posted October 22, 2008 Author Posted October 22, 2008 Yeah I don't like it either but I didn't had time to retouch the calculation. Thanx for the tip.
bruceR Posted October 22, 2008 Posted October 22, 2008 (edited) 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, 2008 by Guest
El_Pablo Posted October 24, 2008 Author Posted October 24, 2008 Nice! You should add the function to the custom function section.
Recommended Posts
This topic is 5875 days old. Please don't post here. Open a new topic instead.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now