Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

How to get from the following intermediate result to the final one with just one pure function?

data = {"2894;Hot Pink;53:09:44;1449714", 
   "17456;Dark Cyan;19:06:42;6929227", "5147;Lime;54:11:55;5247632"};  
  from = 2; to = 3; delim = ";"; nr = to - from + 1;  

inter = StringSplit[#, delim][[from ;; to]] & /@ data  
{{"Hot Pink", "53:09:44"}, {"Dark Cyan", "19:06:42"}, {"Lime", "54:11:55"}}  

final = {{"Hot Pink;53:09:44"}, {"Dark Cyan;19:06:42"}, {"Lime;54:11:55"}}  
share|improve this question
1  
Can't you just join the strings ? You can use Riffle to insert the ;. –  b.gatessucks Dec 28 '12 at 13:35
    
I see that you have not Accepted my answer. If you find it lacking tell me, and I'll try to improve it. If not, please consider Accepting it (green check-mark). See: How does accepting an answer work? –  Mr.Wizard Jan 13 '13 at 4:11
add comment

1 Answer 1

Using only one Function:

{# <> ";" <> #2} & @@@ StringSplit[data, delim][[All, from ;; to]]

Perhaps more robustly:

{"" <> Riffle[#, ";"]} & /@ 
  StringSplit[data, delim][[All, from ;; to]]

Using the Listable form of StringSplit as shown above should be faster in any case.


Just for fun, without Function at all:

StringJoin /@ StringSplit[data, ";" -> ";"][[All, 3 ;; 5]]
{"Hot Pink;53:09:44", "Dark Cyan;19:06:42", "Lime;54:11:55"}

Turning this "fun" method into a usable solution which proves quite fast:

List /@ StringJoin /@ 
  StringSplit[data, delim -> delim][[All, 2 from - 1 ;; 2 to - 1]]
share|improve this answer
    
Very good. . . .Interesting to compare your functional sulution with a algorithmically one. In one setting. . .1.000.000 records were treated. . .Yours: 33.899 sec. .the other: 20.78sec. . .On an differend setting also 1.000.000 records treated. . .Yours: 7.7sec. . .the other: 13.6sec –  Hp Radojewski Schäfer Von Dec 29 '12 at 10:19
    
@HpR Which methods are you comparing? –  Mr.Wizard Dec 29 '12 at 14:13
    
A first method reads in from external source one line by line. Acts on it and append it to disk. Thats for very large segments that wont fit in memory at ones. The second one reads in the whole raw segment. Acts on it and writes it to disk. This is for segments that fit in memory at ones. (and what was ask for in the first place) I used your function unchanged for both methods. –  Hp Radojewski Schäfer Von Dec 30 '12 at 11:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.