×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Adding words to a sentence!

Adding words to a sentence!

Adding words to a sentence!

(OP)
Hi, I'm a beginner and I have a problem.
I have this variable which contains (usually/hopefully) a sentence. I would like to add random words to different places in the sentence. To be more specific, I would like to add a random word [FOO, BAR or COOL] after the fourth, seventh and tenth word of the sentence..
For example:

Sentence:
Tek-Tips is the hot web community where computer professionals meet and talk.

Result:
Tek-Tips is the hot BAR web community where COOL computer professionals meet FOO and talk.

If the sentence was longer, the random word would be placed with intervals of three (actually everywhere in the sentence except in the beginning, where it is placed after the fourth word).
I hope I'm understood.

So, how can this be achieved?? I'm a newbie so I would appreciate an example.

RE: Adding words to a sentence!

seems like I answered this same question a day or two ago...... seems like maybe there is an assignment due sometime soon???? ;^)

Check out splice.  An example can be found in the post in this forum called
Inserting Array Values .... a day or two back...

'hope this helps....

 
 
 
 keep the rudder amid ship and beware the odd typo

RE: Adding words to a sentence!

(OP)
Yeah, it helps.
But what is the fastest way? Could it be done with just one splice?

RE: Adding words to a sentence!

I don't see how.  You could set up a function that cycled through the splices for you, instead of doing the long hand.  But, the splice syntax does not appear to handle more than one splice at a time.

# I think this should work.

# hash of {word to add, new position for word}
%spliceThese = ('word1','2','word2','4','word3','6');
@string = ('a','string','of','words','to','be','played','with','for','fun');

foreach $newWord (keys(%spliceThese))
    {
    $position = '';
    $position = $spliceThese{$newWord};
    splice @string,$position,0,$newWord;
    }


'hope this helps.


 
 
 
 keep the rudder amid ship and beware the odd typo

RE: Adding words to a sentence!

I've got another one:

[code]
$sentence = "I wrote this text to test my fancy word adding function.";  
@words = split(" ",$sentence);
srand;
for ($n = 3; $n < scalar(@words)-1; $n += 3)
 {
    @adds = (' FOO',' BAR', ' MOO');    
    $words[$n] .= $adds[int rand(@adds)];
 }
$it = join(" ",@words);
print $it;
[/code]

In this one, you don't have to know the size of the sentence. After all, TMTOWTDI, right?

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close