Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Escape special characters 1

Status
Not open for further replies.

kitty024

Programmer
Feb 19, 2002
15
GB
Hi

Three questions

1. I've got some data (see below) that contains @{}*, how would i escape these characters.
I tried using quotemeta but but i get the following error messages if i try to print $testString.

Possible unintended interpolation of @Rose in string at prints1.pl line 13
Possible unintended interpolation of @outside in string at prints1.pl line
Name "main::Rose" used only once: possible typo at prints1.pl line 13.
Name "main::eek:utside" used only once: possible typo at prints1.pl line 13.


2. How would i get a list of field names indicated by the first four uppercase letters on each line. Each record is separated by an asterix (*).


3. How would i produce a list of unique field names, ie: the first 4 uppercase letter. That is if the name exists on the list it's not duplicated.

TEST DATA:

$testString = quotemeta("<IPGR
<RIDE
<RICN PDC
<RIIN PAF5175
<ITID
<ITCO original art: print
<ITTI Peldon @Rose - The Hunt @outside an Inn
<ITRP RM/44/113
<ITPA unmounted
<IDES
<IDPH
<IDPE etching
<IDCA
<IDCC A
<IDCV BD
<IDCU C
<IDOV
<IDOH 126mm
<IDOW 165mm
<PROD
<PRMA Briscoe, Arthur John Trevor [artist & engraver]
<ACQU
<ACDA Nov 1960
<ACME purchase
<ACPP Briscoe, Arthur
<ACAN PR1960-278
<ACMF A 1956-1960 Colnaghi, P & D, & Co Ltd (NMM8/1600)
<MLOC
<MLDM 29 Jan 1992
<MLRE storage
<MLLO 0540
<VALU
<MUST
<MUDA 24 Feb 1992
<MURE sighted
<MUPE Dormer, J
<MREF
<MNEG
<MNNO PW5175
<OTHE
<OTNO LCBE L.3
<NOTS
<NOTE Collection A Laver 1-25.
<NOTE Signed by artist.
<NOTE Box Title: H20.4 Etchings - Arthur Briscoe 1943.
*
<IPGR
<RIDE
<RICN PDC
<RIIN PAF5176
<ITID
<ITCO original art: print
<ITTI Wyatt's @{Corner - A coastal village scene}
<ITRP RM/44/114
<ITPA unmounted
<IDES
<IDPH
<IDPE etching
<IDCA
<IDCC A
<IDCV BD
<IDCU C
<IDOV
<IDOH 125mm
<IDOW 162mm
<PROD
<PRMA Briscoe, Arthur John Trevor [artist & engraver]
<ACQU
<ACDA Nov 1960
<ACME purchase
<ACPP Briscoe, Arthur
<ACAN PR1960-278
<ACMF A 1956-1960 Colnaghi, P & D, & Co Ltd (NMM8/1600)
<MLOC
<MLDM 29 Jan 1992
<MLRE storage
<MLLO 0540
<VALU
<MUST
<MUDA 24 Feb 1992
<MURE sighted
<MUPE Dormer, J
<MREF
<MNEG
<MNNO PW5176
<OTHE
<OTNO LCBE L.4
<NOTS
<NOTE Collection A Laver 1-25.
<NOTE Signed by artist.
<NOTE Box Title: H20.4 Etchings - Arthur Briscoe 1943.
*")


Kitty
 
Hello Kitty,

If you enclose your string in single quotes Perl will not try and interpret the special characters.

If you need it to interpret them sometimes then use the \ character to escape them.

Fieldnames - is this data in a file in this format?

Mike

"Deliver me from the bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Kitty,

try this
Code:
#1
$testString=~ s/@/\@/g;
$testString=~ s/{/\{/g;
$testString=~ s/}/\}/g;
$testString=~ s/*/\*/g; #not sure if you'll need this one
#2
@records=split /*/, $testString; #this might need some escaping, not sure
#3
my %fields;
foreach (@records) {
  @keys=split /\n/, $_;
  foreach(@keys)
     $key=substr $_,1,4;
     $fields{$key}++;
  }
  while (($key,$value)=each %fields) {
    if ($value > 1) {
      print "$key is not unique\n";
    }
  }
}

Not tested, but should get you started
--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ... smack the fecker
 
oops ....
subs should be
$testString=~ s/@/\\\@/g; #need to escape backslashes as well
--paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ... smack the fecker
 
Hi Mike

Thanks for the tip about the single quote. I've tried it and now it's saying

Substitution pattern not terminated at prints1.pl line 114.

I guess it's choking on the apostrophe in the word Wyatt's

Secondly, the data is in the format as shown in my original posting ie: each field entry is separated by a new line and each record is separated by an asterix.
It's actually 30 or so text files holding various number of records. I thought i'd try getting it to work with a couple of records first.

regards

kitty





 
Instead of using single quotes 'like this' you can use the other quoting mechanism q/like this/



Mike

"Deliver me from the bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Hi Mike

Your tip about q\\ worked.
I'm totally new to Perl so i don't have the knowledge base that you guys have. I'm now going to try and work on getting the list of unique field names.

many thanks

kitty
 
Hi Paul

Thanks for the code you posted, it does need modifying where you indicated, but i'm using it as a starting point. If you get anymore flashes of inspiration please let me know.

cheers

kitty
 
Kitty,

Have a look at associative arrays, they will give you a way to check if a field is unique or not.

Mike

"Deliver me from the bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top