in reply to Re: Re: take out section of a string then put it back
in thread take out section of a string then put it back
my $score = 'score SOME_WORD 3.0 3.0 3.0 3.0'; my $removed_word = $score =~ s/^(score\s\b\w+\b\s)//; # $removed_word now contains 'score' plus whatever # unknown word you removed, plus trailing single space. # $score now is left only with what comes after the removed word, # and preceding whitespace has been removed. # Now you can do whatever processing you wish. # Then to put it back together: $string = $removed_word . $score;
The supplied regexp captures and then removes 'score', followed by a single whitespace, followed by an unknown "word" (the \b word boundry zero-width assertion may be unnecessary, but to me it makes it clear that you're going for a single word), followed by the single whitespace preceding the number.
$removed_word gets all the stuff we just captured and removed. $string is left with what follows, which, as you state, is the score you want to show on the webpage.
I'm aware that I didn't give you a cut-n-paste snippit, using your variable names, etc. I chose not to because I didn't want to have my example cut-n-pasted into your code, and risk my improperly using your variable names due to not seeing enough of your original code. But if you take a moment and understand what is going on in my example, you'll probably see how to modify your code to get it to do what you want.
I also recommend looking at perldoc perlre (the regular expression Perl documentation page) and perlretut (the regular expression tutorial Perl documentation page). If you have Perl, you have those pages on your computer too. They will help you to broaden your base so that you can work through this type of question on your own.
A good way to get a clear answer is to post a clear question along with a snippet of stand-alone example code to illustrate your question. Try to boil your example code down to the lowest common denominator. If all you're asking is how to extract a portion of a string, and then delete the portion that you extracted, post sample code that creates the string, and illustrates what you've attempted in reaching your goal, uncluttered by mysterious arrays and variable names that are peripheral to the core question. Often when you take this little step of isolating your question and illustrating it in stand-alone code, you will find the answer yourself along the way. This is just a suggestion, but I've found many times that it works for me.
One more tip. If you're taking keyed input directly from a user on the web and writing it out to a file, you should realize that the input is tainted until you've thoroughly washed it. When you write user input to a file, you're opening the back door to trouble. At very minimum, run your script with the -T option (Taint checking).
Dave
"If I had my life to do over again, I'd be a plumber." -- Albert Einstein
|
|---|