String to array

Discussion in 'RGSSx Script Support' started by Yuuta Kirishima, May 11, 2016.

  1. Yuuta Kirishima

    Yuuta Kirishima Software Engineer/Professional Mayonnaise Player Veteran

    Messages:
    548
    Likes Received:
    131
    Location:
    North Carolina
    First Language:
    Engilsh
    Primarily Uses:
    RMMV
    I request the help of this community on how to properly convert a string into an array, A bunch of confusing code that ultimately returns a variable named team from the server, inside this team variable is the value: 


    "[9, 27, 32, 112, 4]"


    I need this turned into the array


    [9, 27, 32, 112, 4]


    I've tried the following:


    aTeam = team.tr('[]', '')
    vTeam = aTeam.split


    did not work, Ruby must have inherently included quotes or something.


    Any help would be greatly appreciated
     
    Last edited by a moderator: May 11, 2016
    #1
  2. ??????

    ?????? Diabolical Codemaster Veteran

    Messages:
    6,264
    Likes Received:
    2,310
    Location:
    Your OS
    First Language:
    Binary
    Primarily Uses:
    RMMV
    Code:
    def stringToArray(aString)
      return aString.tr('[]','').split(',').map{|e|e.to_i}
    end
    
    print stringToArray("[9, 27, 32, 112, 4]");
     
    #2
    Yuuta Kirishima and Zeriab like this.
  3. Hudell

    Hudell Dog Lord Veteran

    Messages:
    3,296
    Likes Received:
    2,869
    Location:
    Brazil
    First Language:
    Portuguese
    Primarily Uses:
    RMMV
    Code:
    JSON.parse('[9, 27, 32, 112, 4]');
     
    #3
    ?????? likes this.
  4. ??????

    ?????? Diabolical Codemaster Veteran

    Messages:
    6,264
    Likes Received:
    2,310
    Location:
    Your OS
    First Language:
    Binary
    Primarily Uses:
    RMMV
    #4
    Zeriab likes this.
  5. Hudell

    Hudell Dog Lord Veteran

    Messages:
    3,296
    Likes Received:
    2,869
    Location:
    Brazil
    First Language:
    Portuguese
    Primarily Uses:
    RMMV
    Ooops
     
    #5
    Zeriab, EternalShadow and ?????? like this.
  6. Yuuta Kirishima

    Yuuta Kirishima Software Engineer/Professional Mayonnaise Player Veteran

    Messages:
    548
    Likes Received:
    131
    Location:
    North Carolina
    First Language:
    Engilsh
    Primarily Uses:
    RMMV
    Lovely! You just saved a stressful battle system
     
    #6
  7. Adventurer_inc.

    Adventurer_inc. Technically a Programmer Veteran

    Messages:
    85
    Likes Received:
    38
    Location:
    USA
    First Language:
    English
    Primarily Uses:
    RMMV
    #7
  8. Yuuta Kirishima

    Yuuta Kirishima Software Engineer/Professional Mayonnaise Player Veteran

    Messages:
    548
    Likes Received:
    131
    Location:
    North Carolina
    First Language:
    Engilsh
    Primarily Uses:
    RMMV
    #8
  9. Sixth

    Sixth Veteran Veteran

    Messages:
    2,121
    Likes Received:
    789
    First Language:
    Hungarian
    Primarily Uses:
    RMVXA
    Why not simply use:


    def string_to_array(string)
    ary = []
    string.scan(/\d+/).each {|num| ary << num.to_i}
    return ary
    end


    You just need the numbers from the string, right? This seems much more simpler than using .tr than .split and than .map to get the numbers.


    This will, of course, only work if the string you have can only contain needed numbers, and not some irrelevant numbers which you don't need in your array.
     
    #9
    Zeriab likes this.
  10. ??????

    ?????? Diabolical Codemaster Veteran

    Messages:
    6,264
    Likes Received:
    2,310
    Location:
    Your OS
    First Language:
    Binary
    Primarily Uses:
    RMMV
    That's unnecessarily lengthy.. Could easily shorten it...


    def stringToArray(aString)
    return aString.scan(/\d+/).reduce([]){|r,i|r<<i.to_i}
    end



    I don't think that would make much difference over my previously suggested solution though. :)


    Edit:


    Also, eval should never be used when there is a more elegant solution available.
     
    Last edited by a moderator: May 12, 2016
    #10
  11. Zeriab

    Zeriab Huggins! Veteran

    Messages:
    1,198
    Likes Received:
    1,250
    First Language:
    English
    Primarily Uses:
    RMXP
    Of the solutions presented here I find the eval the most elegant solution, but at the same time it is clearly the worst.


    As Yuuta mentions, the data comes from a server. I.e. the string is tainted. Never ever evaluate tainted strings. This is a far bigger reason than any other for not using the eval solution.



    @Dekita


    I disagree.  I consider your revision unnecessarily convoluted. Sixth's code clearly communicates what it does. The variable naming reflects what they represent. All of this is small stuff, I know. It's just, I consider going from your version to Sixth's version a good refactoring.


    The difference between the two good solutions is quite interesting. The solution by Dekita focuses tearing and splitting according to the specific way the values are packaged, parsing the resulting clumps of strings as integers. The solution by Sixth focuses on the consecutive groups of digits parses the integers without regards to how the values have been packaged. There are pros and cons with each solutions. Removing and adding suffixes and prefixes, altering separation characters. This affects one solution, not another.


    The digit scan is more robust to changes in the surrounding chars while the requirement of integers separated by any chars exists. Adding decimals? And the other solution works without changes.


    Want to add nesting? Again one solution is easier to modify than the other.


    Interesting, right? :3
     
    #11
    ?????? likes this.
  12. ??????

    ?????? Diabolical Codemaster Veteran

    Messages:
    6,264
    Likes Received:
    2,310
    Location:
    Your OS
    First Language:
    Binary
    Primarily Uses:
    RMMV
    @Zeriab I just spent the best part of an hour pondering the implications of this, then I remembered I have actual things to do :D


    Side note: I always enjoyed ruby for how easy it is to have all the logic contained on one line, not sure why, but yea.. :)
     
    #12
  13. Tsukihime

    Tsukihime Veteran Veteran

    Messages:
    8,230
    Likes Received:
    3,055
    Location:
    Toronto
    First Language:
    English


    It's also enjoyable to read other people's code golf. Hopefully I never have to modify such code since I don't want to spend an hour wondering what it's actually supposed to do.


    The only reason why I might choose not to write out the logic myself is if the engine can perform optimizations to speed up the operation, as opposed to me manually parsing and building up the data myself.
     
    #13

Share This Page