YetAnotherForum
Welcome Guest Search | Active Topics | Log In | Register

New Topic Post Reply
Split string to integer array
mindchamber
#1 Posted : Sunday, May 21, 2017 5:41:42 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 5/21/2017(UTC)
Posts: 4

Thanks: 3 times
Was thanked: 0 time(s) in 0 post(s)
Hi, first sorry for my english)

I have string:
a = "1;2;3;4;5"

When i use split function ( split(a, ;) ) , i have this array:
a = ["1","2","3","4","5"]

how to transform type of array elements to integer?

Thanks.
Guest
#2 Posted : Sunday, May 21, 2017 8:25:14 PM(UTC)
Rank: Guest

Groups:
Joined: 7/25/2010(UTC)
Posts: 20

Thanks: 0 times
Was thanked: 1 time(s) in 1 post(s)
foreach (index,element in a) {
a[index]=element.tointeger() ;
}

1 user thanked Guest for this useful post.
mindchamber on 5/22/2017(UTC)
mindchamber
#3 Posted : Sunday, May 21, 2017 10:57:42 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 5/21/2017(UTC)
Posts: 4

Thanks: 3 times
Was thanked: 0 time(s) in 0 post(s)
Thank you!
es
#4 Posted : Wednesday, May 24, 2017 7:06:25 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 2/13/2017(UTC)
Posts: 3

Thanks: 0 times
Was thanked: 2 time(s) in 2 post(s)
You might prefer something made for this task:
Code:
a.apply(@(v) v.tointeger());
1 user thanked es for this useful post.
mindchamber on 5/26/2017(UTC)
mindchamber
#5 Posted : Thursday, May 25, 2017 6:15:54 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 5/21/2017(UTC)
Posts: 4

Thanks: 3 times
Was thanked: 0 time(s) in 0 post(s)
es wrote:
You might prefer something made for this task:
Code:
a.apply(@(v) v.tointeger());

It will be more productive than a cycle?
es
#6 Posted : Thursday, May 25, 2017 6:34:36 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 2/13/2017(UTC)
Posts: 3

Thanks: 0 times
Was thanked: 2 time(s) in 2 post(s)
mindchamber wrote:
es wrote:
You might prefer something made for this task:
Code:
a.apply(@(v) v.tointeger());

It will be more productive than a cycle?

Yes because this is exactly why apply was made, and lambdas are officially recommended by the Squirrel manual to be used like this.

"Lambda expressions are a synctactic sugar to quickly define a function that consists of a single expression. This feature comes handy when functional programming patterns are applied, like map/reduce or passing a compare method to array.sort()."
('apply' internally uses 'map' and replaces the array with the return value from it. The code above is the equivalent of this:
Code:
a = a.map(@(v) v.tointeger());
)

This is also functionally equivalent code to the foreach statement suggested above but is a lot neater and focuses on what to do rather than how to do it.

However, if you prefer imperative programming over declarative, you would be fine with foreach.
1 user thanked es for this useful post.
mindchamber on 5/26/2017(UTC)
mindchamber
#7 Posted : Thursday, May 25, 2017 8:13:59 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 5/21/2017(UTC)
Posts: 4

Thanks: 3 times
Was thanked: 0 time(s) in 0 post(s)
Thank you very much, sir!
Quick Reply Show Quick Reply
Users browsing this topic
Guest
New Topic Post Reply
Forum Jump  
You can post new topics in this forum.
You can reply to topics in this forum.
You can delete your posts in this forum.
You can edit your posts in this forum.
You cannot create polls in this forum.
You can vote in polls in this forum.

Clean Slate theme by Jaben Cargman (Tiny Gecko)
Powered by YAF 1.9.4 | YAF © 2003-2010, Yet Another Forum.NET
This page was generated in 0.281 seconds.