LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 11-03-2011, 05:19 AM   #1
qrange
Senior Member
 
Registered: Jul 2006
Location: Belgrade, Yugoslavia
Distribution: Debian stable/testing, amd64
Posts: 1,032

Rep: Reputation: 47
perl, multiple string replacement


What would be most elegant way of combining these replace commands:

$title =~ s/sky/Sky/g;
$title =~ s/water/Water/g;
$title =~ s/earth/Earth/g;
..
?

thanks.
 
Old 11-04-2011, 06:52 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Why do you need to combine them? They're quite readable as they are. The only way you could elegantise it as far as I know is to do something like

Code:
for($title){
    s/sky/Sky/g;
    s/water/Water/g;
    s/earth/Earth/g;
}
Hope this helps,
 
Old 11-04-2011, 09:41 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,848

Rep: Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012
I'm just a beginner in perl, but gnu sed has \u (uppercase one letter) and \U (uppercase all letters) flags for the replacement field. It appears to work with perl too.

Code:
$title =~ s/(sky|water|earth)/\u$1/g ;
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash string replacement t4sked Linux - General 5 05-14-2009 01:57 AM
Multiple string Replacement Abhilasha Linux - Newbie 4 04-12-2007 08:19 AM
string padding and replacement functions George2 Programming 4 11-19-2006 03:09 AM
Shell Script - String Replacement revof11 Programming 7 11-29-2005 07:38 AM
Perl string replacement within an array? Seventh Programming 1 09-07-2004 03:50 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration