Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Add array range to new array (https://forums.graalonline.com/forums/showthread.php?t=82997)

LoneAngelIbesu 12-04-2008 03:37 AM

Add array range to new array
 
To get back in to scripting things other than GUIs and HP systems, I've decided to try and recreate PHP's explode() and implode() functions. I'm wondering, however, is there already an existing way to add an array range to a new array? Or will I have to script my own?

For instance, explode() has three parameters: delimiter, string, limit. If limit is set to a positive number that is less than the string's length when tokenized by the delimiter, it will add each token, with the last one containing the rest of the string. So, if my string is "1,2,3,4,5", my delimiter is ",", and my limit is 3, the output would be: {1, 2, "3,4,5"}.

Tigairius 12-04-2008 04:10 AM

I don't believe there is already an explode() or implode() function.

I'd imagine you could do one like this:
PHP Code:

echo(explode(",""1, 2, 3, 4, 5, 6"3)); 

PHP Code:

function explode(delstrlimit) {
  
temp.token str.tokenize(del);
  for (
temp.0temp.token.size(); temp.++) {
    if (
limit) {
      
temp.output.add(token[i]);
    }else {
      
temp.positions str.positions(del);
      
temp.output.add(str.substring(positions[1] + 1));
      break;
    }
  }
  return 
output;


Good luck.

LoneAngelIbesu 12-04-2008 04:37 AM

Quote:

Originally Posted by Tigairius (Post 1446584)
PHP Code:

    }else {
      
temp.positions str.positions(del);
      
temp.output.add(str.substring(positions[1] + 1));
      break;
    }
  } 


That's exactly what I was looking for. Thank you, Tig. Though, for anybody thinking about using the script Tig provided: it's not the complete explode() function, as in PHP. I'll release mine publicly as soon as I finish it.

The_Kez 12-04-2008 07:14 PM

What is explode be useful for? I've only ever done work in GS2 so this sort of stuff I've never heard of before. :p

ApothiX 12-04-2008 08:24 PM

Quote:

Originally Posted by The_Kez (Post 1446728)
What is explode be useful for? I've only ever done work in GS2 so this sort of stuff I've never heard of before. :p

It converts a deliminated string to an array. Useful if you want to work with individual portions of a string without using messy string manipulation functions.


All times are GMT +2. The time now is 09:34 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.