Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Needing to take out 3rd word of sting

by Rich36 (Chaplain)
on Sep 24, 2003 at 19:58 UTC ( #293989=note: print w/replies, xml ) Need Help??


in reply to Needing to take out 3rd word of sting

A different way of approaching the problem - use a hash to store the order of the words and the words themselves. This makes it easy to delete or add words in the order that you want. This makes use of a property of delete() that I didn't realize existed - it returns the value of the hash element that it deletes.

my $string = "This is a only a test"; my %words; my $i = 1; # Assign the order of the words as a key, the word itself as # a value $words{$i++} = $_ foreach(split(' ', $string)); # Delete the word that you want to delete my $deleted_word = delete($words{3}); # Put the word back in $words{3} = $deleted_word; # Join the values back together based on the order of # the keys $string = join(" ", map { $_ = $words{$_} } sort keys %words); print $string . "\n";

«Rich36»

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://293989]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2021-04-13 00:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?