RMRK is retiring.
Registration is disabled. The site will remain online, but eventually become a read-only archive. More information.

RMRK.net has nothing to do with Blockchains, Cryptocurrency or NFTs. We have been around since the early 2000s, but there is a new group using the RMRK name that deals with those things. We have nothing to do with them.
NFTs are a scam, and if somebody is trying to persuade you to buy or invest in crypto/blockchain/NFT content, please turn them down and save your money. See this video for more information.
About string split method

0 Members and 1 Guest are viewing this topic.

**
Rep:
Level 68
RMIDans ~
Back then, when I started to learn how to read notetag, I used yanfly's way to split string each line in notetag through this function.
Code: [Select]
self.note.split(/[\r\n]+/).each do |line|
I didn't know how its work. And I just follow.

Now, I just realize that if I want to split string each line, It can be done by write this command
Code: [Select]
self.note.split(/\n/).each do |line|

The question is, what's the difference between those two commands?
My scripts collection :
http://theolized.blogspot.com/
Sorry if I made it in Indonesian

Supporter of :


Make your own party composition based on 20 playable characters :

*
Rep:
Level 82
The visible differences pretty much sums it up.

In Yanfly's version, the string is split at a line feed (\n) or carriage return (\r). Your version only takes into account line feeds and not carriage return. The \r\n is a commonly seen implementation that comes from the DOS days, I believe.

I would stick to the traditional version that Yanfly uses.

There's probably other more subtle differences that others, like modern, may be able to comment on. I'm not an expert on regular expressions to offer much insight (and I don't have the time at the moment to research into it).
(Why do I always feel like it's the end of the world and I'm the last man standing?)

*
? ? ? ? ? ? ? ? ? The nice kind of alien~
Rep:
Level 92
Martian - Occasionally kind
The string.split method partitions into elements according to the specified separator.
Consider a comma separated file (CSV) which you for example can open with Excel and it splits the elements in a line neatly into different rows. It does not take the actual commas along. This is analogous with how the string split method works. The separator is not added to elements in the array. Now instead of a specific character we define separator by a regular expression where Yanfly uses [\r\n]+

First step to understanding the different between the two lines is to understand what the regular expression matches. A good heuristic is to decompose to the simplest entities and build up.
We have [ ] with the meaning of (any character in the set). If we for example have /[ab]/ then it is equivalent to /a|b/.
There are two possible matches "a" and "b".

The + means 1 or more repetitions.  /a+/ matches "a", "aa", "aaa", ...
Since the [] is treated as a single entity for the + we have that /[ab]+/ matches for example "abbaba", "baab" and "a".
Substitute a and b with \n and \r to get Yanfly's equation.
We can now easily see that a separator is defined as any non-empty substring containing only line feed and carriage return characters.

Compare with your line where a separator is a single line feed.
Including the carriage return is considered good practice because you would then cover both the Windows and Unix cases.
The important part, however, is considering a separator to be specifically one line feed character.
This is important because of the way the string.split method treats separators.

Try to figure it out on your own how it works. When you feel ready, try to guess what the following code will output before running it:
Code: [Select]
str = "Test 1\nTest 2\r\n\n\n\r\n5\n\r642345\n2gj"

arr1 = []
arr2 = []

str.split(/[\r\n]+/).each do |line|
  arr1 << line
end

str.split(/\n/).each do |line|
  arr2 << line
end

str1 = arr1.join
str2 = arr2.join

p str, str1, str2, arr1, arr2
#msgbox_p str, str1, str2, arr1, arr2 # Use if run in VX Ace
exit


Remember that one is not necessarily better than the other. Not in general. Which to pick depends on your specific context.

*hugs*
 - Zeriab

*
Rep:
Level 82
Thanks for the input Zeriab. I always enjoy reading.

You have this very strange mystical presence when you descend to share your wisdom and knowledge. I feel bad for not remembering to include you as one of those who will know more about the subject.
(Why do I always feel like it's the end of the world and I'm the last man standing?)

**
Rep:
Level 68
RMIDans ~
Sorry for late reply. I almost forgot about this thread. Btw, that's explains a lot. I like to study by examples, experiments and its outputs. And make my own conclusion and explanation. Now I understand lil bit more about regular expressions. Thanks very much...

*solved*
My scripts collection :
http://theolized.blogspot.com/
Sorry if I made it in Indonesian

Supporter of :


Make your own party composition based on 20 playable characters :