strsplit

How to avoid a loop in R: selecting items from a list

I could solve this using loops, but I am trying think in vectors so my code will be more R-esque. I have a list of names. The format is firstname_lastname. I want to get out of this list a separate list with only the first names. I can't seem to get my mind around how to do this. Here's some example data: t <- c("bob_smith","mary_jane...

hangman "word" in php

I am trying to do something similar to hangman where when you guess a letter, it replaces an underscore with what the letter is. I have come up with a way, but it seems very inefficient and I am wondering if there is a better way. Here is what I have - <? $word = 'ball'; $lettersGuessed = array('b','a'); echo str_replace( $lettersGues...

R: chopping a string into a vector of character elements

I have an object containing a text string: x <- "xxyyxyxy" and I want to turn that into a vector with each element containing two letters: [1] "xx" "yy" "xy" "xy" it seems like the strsplit() should be my ticket, but since I have no regular expression foo, I can't figure out how to make this function chop the string up the way I wa...

R strsplit and vectorization

When creating functions that use strsplit, vector inputs do not behave as desired, and sapply needs to be used. This is due to the list output that strsplit produces. Is there a way to vectorize the process - that is, the function produces the correct element in the list for each of the elements of the input? For example, to count the l...

PHP - I need to split a large string into an array, but the split point cannot break a tag

I wrote a script that sends chunks of text of to google to translate, but sometimes the text, which is html source code) will end up splitting in the middle of an html tag and google will return the code incorrectly. I already know how to split the string into an array, but is there a better way to do this while ensuring the output stri...

apply strsplit rowwise

Dear all, Im trying to split a string on "." and create additional columns with the two strings before and after ".". tes<-c("1.abc","2.di","3.lik") dat<-c(5,3,2) h<-data.frame(tes,dat) h$num<-substr(h$tes,1,1) h$prim<-unlist(strsplit(as.character(h$tes),"\\."))[2] h$prim<-sapply(h$tes,unlist(strsplit(as.character(h$tes),"\\."))[2]) ...