Skip to content
View in the app

A better way to browse. Learn more.

FMForums.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Replace element at index X in a list?

Featured Replies

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.

you mean a return separated list?

Cat

Dog

Mouse

you want to replace dog at value position 2 with Horse?

  • 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")

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.

  • 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 )

  )

)

Not in MY calculation, surely?

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)

)

)

  • Author

Yeah I don't like it either but I didn't had time to retouch the calculation.

Thanx for the tip.

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 by Guest

  • Author

Nice!

You should add the function to the custom function section.

Create an account or sign in to comment

Important Information

By using this site, you agree to our Terms of Use.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.