in reply to how do I use s///e
my %myHash = (hello=>"Howdy", goodbye=>"See ya"); while (<DATA>) { s/\$(\w+)/$myHash{$1}/eg; print; } __DATA__ When I want to say hello I say $hello. When I want to say goodbye I say $goodbye!
Update Whoops! Thanks Abigail! This started as an example that used a sub to do the look up in a database but I got all lazy and just did it as a hash. Some revised (incomplete) code below.
s/\$(\w+)/dbLookup($1)/eg; sub dbLookup { # Do database lookup (which would take a few more lines!) return $lookedup ? $lookedup : ''; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: how do I use s///e
by Abigail-II (Bishop) on Jan 26, 2004 at 17:43 UTC | |
by ysth (Canon) on Jan 26, 2004 at 23:16 UTC |