puddleglum has asked for the wisdom of the Perl Monks concerning the following question:
I have a large tab-delimited data file:
e.g. Town\tCounty Westminster\tGreater London King's Cross\tGreater London Mayfair (north)\tGreater London Mayfair (south)\tGreater London ...
what i am doing is a count of some things, then adding a value to each list item;
Town\tCounty\tVALUE
love perl for how easy this (generally) is;
$tmp=(get the file contents); $tmp=~s/\r//g; $tmp="\n$tmp\n"; ($n,$v)=[got from somewhere, same as list items] ($ctr)=[got from somewhere else] ($rctr)=[count of replacements made] ## attempt 1 if ($tmp=~/^$n\t$v$/) { $tmp=~s/^$n\t$v$/$n\t$v\t$ctr/; $rctr++; } else { ....
THE PROBLEM is that if $n contains brackets () or a single quote ', then it doesn't match. Now, the following fixes the brackets, it matches fine, BUT NOT THE QUOTE. I cannot match or replace the list items with single quotes (apostrophes):
# attempt 2 $n=~s/\(/\\(/g; ## << this works ok! $n=~s/\)/\\)/g; $n=~s/\'/\\'/g; ## << same method, NO WORK!</font> if ($tmp=~/^$n\t$v$/) { $tmp=~s/^$n\t$v$/$n\t$v\t$ctr/; $gctr++; }
How do i fix this? Anyone? Help gratefully appreciated!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Not matching a single quote (apostrophe!)
by moritz (Cardinal) on Nov 17, 2009 at 13:37 UTC | |
by puddleglum (Initiate) on Nov 17, 2009 at 14:03 UTC | |
by moritz (Cardinal) on Nov 17, 2009 at 14:40 UTC | |
|
Re: Not matching a single quote (apostrophe!)
by JavaFan (Canon) on Nov 17, 2009 at 14:06 UTC | |
by puddleglum (Initiate) on Nov 17, 2009 at 14:24 UTC | |
|
Re: Not matching a single quote (apostrophe!)
by przemo (Scribe) on Nov 17, 2009 at 13:38 UTC | |
|
Re: Not matching a single quote (apostrophe!)
by puddleglum (Initiate) on Nov 17, 2009 at 14:43 UTC | |
by ikegami (Patriarch) on Nov 17, 2009 at 16:00 UTC | |
by puddleglum (Initiate) on Nov 18, 2009 at 00:07 UTC |