SAIL - ReplaceIn

Gotowe kody SAIL'a, informacje na temat moddingu, itd.

Moderator: zoNE

Post Reply
User avatar

Topic author
zoNE
The Great Uniter & Site Administrator
The Great Uniter & Site Administrator
Posts: 1524
Joined: Fri Feb 17, 2006 3:44 pm
Location: Poland
Contact:

SAIL - ReplaceIn

#1

Post by zoNE » Mon Aug 07, 2017 4:39 pm

Serpent wrote:
Mon Aug 07, 2017 2:43 pm
Hi, napisałem funkcję która podmienia wartość w podanym miejscu, w dowolnej tablicy wielowymiarowej.

Code: Select all

// arr - tablica
// pos - tablica pozycji
// value - nowa wartość do wstawienia
Export Function ReplaceIn(arr, pos, value);
var i, tmp, s_arr;
begin
if pos < 1 then
   exit;

if pos = 1 then
   result := Replace(arr, pos[1], value)
    else
     begin      
     tmp := arr;
     s_arr := [tmp];

     for i = 1 to pos-1 do
         begin  
         tmp := tmp[pos[i]];
         s_arr := s_arr ^ [tmp];
         end;

     tmp := Replace(tmp, pos[pos], value);
     s_arr := Replace(s_arr, s_arr, tmp);

     for i = s_arr downto 2 do
         begin
         tmp := Replace(s_arr[i-1], pos[i-1], s_arr[i]);
         s_arr := Replace(s_arr, i-1, tmp);
         end;

     result := s_arr[1];
     end;

End;
Przykład użycia:

Code: Select all

t := [ [1, 2], [ [15, 3], [1, [11, 12]], [3, 4]] ];
t := ReplaceIn(t, [2, 2, 2, 2], 88);
display_strings := t;
Wyświetli [ [1, 2], [ [15, 3], [1, [11, 88]], [3, 4]] ].
Author: Serpent
Topic: viewtopic.php?f=42&t=4945

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest