mr_evans2u has asked for the wisdom of the Perl Monks concerning the following question:

I am needing to take out the first word of a sting so users can modify a score on the web. After the submit I need to put that word (what ever it may be)back into the string followed by the modified score. I don't have any idea on how to do this, Please help.
elsif ($temp =~ (m/^score/i)) { $item_line[$head_count] = $item_line[$head_count] . $& . $'; $item_line[$head_count] =~ s/score //g; ##### need something to parse out unknow word in string here. $head_count ++; $item_count = 0; } ##### writing the file ############### while($a<=$ttl) { $line_count = 0; $write_header = $tmp_headers[$a]; $write_if = $if[$a]; $write_action = $act[$a]; # Write Section Header print FILE "$write_header\n"; # Write If information print FILE "$write_if\n"; # Create item list for the individual section write_items($tmp_headers[$a]); my $t = -1; #### need to put parsed section of string back between $score and $ite +m_line while ($t++ <= $line_count) { if ($item_line[$t] ne "") { print FILE "$score$item_line[$t]\n\n"; } } $a++; }#end while

janitored by ybiC: Balanced <code> tags around code block, as per Monastery convention

Replies are listed 'Best First'.
Re: take out section of a string then put it back
by davido (Cardinal) on Sep 13, 2003 at 20:59 UTC
    I think I have an idea of how to accomplish what you're looking for. Since your code doesn't make clear what your exact need is I'll just do an example from scratch, and you can adapt it easily to work within the framework of your code. I'm making the assumption that your first word will always match \w+. If it won't, you can switch ^\w+\b with ^.+?\b .....I think. ;)

    Here's the example:

    my $string = "Here is our test string."; my $word; ( $word, $string ) = split /^(\w+)\b/, $string, 2;

    Now you have $word containing the first word found in $string, assuming a word, to you, contains nothing but word characters. And $string now contains only everything after the first word (including the leading space).

    To put the word back, just concatenate:

    $string = $word . $string;

    This could also be done with a regular plain vanilla pattern match in several ways, but I'll do it the easy way:

    my $string = "This is the test string."; my $word; ( $word, $string ) = $string =~ m/^(\w+)\b(.*)/;

    As you can see above, we don't really care about greediness of .* because we want it to be greedy enough to grab everything following the first word. If you have newlines within the string you'll have to use the /s modifier.

    Here's another approach with substitution:

    my $string = "Here is our test string."; my $word = $string =~ s/^(\w+)\b(.*)/$2/;

    Now you've pushed the first word into $word, and captured everything else in $2, which then becomes the substitute for the original string. Again, we don't mind of .* is greedy as long as it leaves enough for a word at the beginning of the string followed by a word boundry. Again, if there is a newline in the string, you'll need the /s modifier on the regexp.

    With all of the examples above, you put humpty dumpty back together again by concatenating:

    $string = $word . $string;

    I hope this helps!

    Dave

    "If I had my life to do over again, I'd be a plumber." -- Albert Einstein

      Thanks for the reply guys but I guess I didn't make my self very clear, so I will try again. I get a string from the array:  $item_line[$head_count] = $item_line[$head_count] . $& . $'; Which I have no idea what it is. From this screen I am already taking out "score". I know this word will always be first. The next word which should be the first word now is unknown. It does have to be all capital letters with the option of numbers and an underscore. This is the word that I don't want the users to see when they go to modify the scores. Example of how it looks in the file: score SOME_WORD 3.0 3.0 3.0 3.0 How I want it to look to the user in the web page: 3.0 3.0 3.0 3.0 I'm not doing $sting = "score SOME_WORD 3.0 3.0 3.0 3.0" so the /^(\w+) isn't working. Once the score has been modified and they hit submit I need to be able to put SOME_WORD back into the file. Currently I'm doing it like this:
      my $t = -1; while ($t++ <= $line_count) { if ($item_line[$t] ne "") { print FILE "$score$item_line[$t] ##$score = score and $item_line[$t]= SOME_WORD 3.0 3.0 3.0 3.0(for thi +s example) \n\n"; } }
      I hope this makes more sence, if not I'll try again Thanks
        Your followup post still is a little vague, but it makes it look as though you want to remove the word 'score' and the very next word that follows it. And then you want to modify the actual number, and finally insert the stuff you chopped off back into the string. ...more or less... See the following example, and then adapt its philosophy for your own situation.

        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

Re: take out section of a string then put it back
by bradcathey (Prior) on Sep 13, 2003 at 20:04 UTC
    Sorry to say, I'm having a hard time understanding what it is you are doing, but if you want to store that first word:

    #!/usr/bin/perl -w use strict; my $temp = "Firstword and then the rest of the string"; $temp =~ /^(\w+)/i; my $firstword = $1;
    BTW, it's more efficient to use groups--the stuff in parens--in your reg exp instead of $`,$&,$'. So:

    my $temp = "Firstword and then the rest of the string"; $temp =~ /^(\w+)([ \w]*)/i; my $firstword = $1; my $restofstring = $2;


    Anyone else?
Re: take out section of a string then put it back
by Not_a_Number (Prior) on Sep 13, 2003 at 20:15 UTC

    I'm afraid this isn't very clear. Perhaps you should give us some examples of:

    1) Your input (a few lines will suffice). Please indicate what you mean by "that word (whatever it might be)".

    2) Your desired output input based on this input.

    3) If relevant, the wrong output that your current programme gives.

    dave