zBernie has asked for the wisdom of the Perl Monks concerning the following question:
I'm trying replace names in a file, with names from a list included in __DATA__ in the script
below. Now assume I have a text file which contains hundreds of
different names, and I want to replace certain ones with a mapping of say:
JOE -> JOHN
FRED -> FREDERICK
SAM -> SAMANTHA
Do I need to create a hash with the 6 names above before I can replace instances of
JOE, FRED, and SAM, in a text file?
#!/usr/bin/perl -w use strict; if ($#ARGV < 0) { print "\nUsage: $0 <File>\n\n"; exit 0; } my $userlist = qq{$ARGV[0]}; open(FILE, qq{$userlist}) or die("$!: $userlist"); my @users = <FILE>; close(FILE); while ( <DATA> ) { print $_; } __DATA__ JOE FRED SAM
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to replace strings in a file from a list?
by toolic (Bishop) on Oct 03, 2012 at 02:21 UTC | |
|
Re: How to replace strings in a file from a list?
by Kenosis (Priest) on Oct 03, 2012 at 03:49 UTC |