Jump to content

Replace function, mutiple replaces like the substitute?


This topic is 3172 days old. Please don't post here. Open a new topic instead.

Recommended Posts

Hi Guys,

I've got a script which I need to replace multiple items. I can't use the substitute command as I need to be precise in what I am replacing, so I have a let function which determines the position and length of items and want to use the replace function. This all seems to work well, however I have come at a cross road where I need to replace 3 items in my $variable, so I have placed 3 replace functions in my script. The problem is my output is also tripling, just want to output 1 set of data.

 

Let([
    
   I declare all my variables which obtain the start and length...


];

Case(
    // If its the first one in the loop keep the start date but replace the enddate
    $j = 0 ; Replace ( $variable ; pos_endtime ; length_endtime ; "18:00:00" ) ;

 

/* THIS IS WHERE I AM HAVING TROUBLE I WANT TO REPLACE 3 ITEMS in my $variable*/ 
    // If its greater than the first day and meets the last day replace start date
    // Start Date needs to be incremented
    $j > 0 and $j = $day_count ;
    
    Replace ( $variable ; pos_endtime ; length_endtime ; $endtime ) &
    Replace ( $variable ; pos_starttime ; length_starttime ; "8:00:00" ) &
    Replace ( $variable ; pos_startdate ; length_startdate ; start_date_new ) ;


    $j <> 0 and $j <> $day_count ;
    Replace ( $variable ; pos_startdate ; length_startdate ; new_date ) and
    Replace ( $variable ; pos_starttime ; length_starttime ; "8:00:00" ) &
    Replace ( $variable ; pos_endtime ; length_endtime ; "18:00:00" ) ; 

)

 


)

 

Thanks in advance

Link to comment
Share on other sites

This is very confusing and impossible to follow.

In general, if you want to replace multiple strings in a given text, you need to nest the Replace() function calls - for example:

Replace ( Replace ( "hh:mm" ; 1 ; 2 ; "12" ) ; 4 ; 2 ; "45" )

returns "12:45".

By using the Let() function, you can do the replacements in series, which makes for a clearer code. Here's an example of the same thing as before:

Let ( [
text = "hh:mm" ;
result = Replace ( text ; 1 ; 2 ; "12" ) ;
result = Replace ( result ; 4 ; 2 ; "45" )
] ;
result
)

 

Important:
Each Replace() operates on the result of the preceding Replace()) calls. You must take this into consideration when providing the start and numberOfCharacters parameters, since the replacementText can easily have different length than the text being replaced. For example:

Let ( [
text = "hh:mm" ;
result = Replace ( text ; 1 ; 2 ; "9" ) ;
result = Replace ( result ; 4 ; 2 ; "45" )
] ;
result
)

returns "9:m45" instead of the naively expected "9:45".

 

1 hour ago, Jalz said:

I can't use the substitute command as I need to be precise in what I am replacing

I believe that's worth reconsidering.

 

 

Link to comment
Share on other sites

2 hours ago, Jalz said:


    Replace ( $variable ; pos_endtime ; length_endtime ; $endtime ) &
    Replace ( $variable ; pos_starttime ; length_starttime ; "8:00:00" ) &
    Replace ( $variable ; pos_startdate ; length_startdate ; start_date_new ) ;

 

Just to pick one thing out:  the use of "&" is to concatenate parts of a string, not to execute commands in succession.  The commands will get executed but the result of the first will be concatenated with the result of the 2nd and 3rd.

Link to comment
Share on other sites

Maybe it would be easier to declare multiple variables, with simpler chunks of data, which you could then manipulate as needed and assemble at the end. That's the beauty of using a script, you don't have to cram all your logic into a single god object.

Link to comment
Share on other sites

This topic is 3172 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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

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