D
dscastroii
I'm trying to replace ascii codes (or &entities) in a text file with
it's equivalent character using a hash. The problem I'm running into
is I am not allowed to use a foreach or while loop to do this (yes,
this is a homework assignment). I must use the -p0777 option with the
#!/usr/bin/perl -p0777 which provides the looping and print function.
I must do this using one line of code only. I must use the /sg
options in the replace statement to satisfy the instructor's
requirements.
The hash table consists of %entity = ( lt=> '<', gt=> '>', amp=> '&',
'#65'=> 'A', quot=> '"', nbs => chr 160);
I need to loop through a text file that contains these &entities of
variable characters and lengths like so:
& hello
< bye
A sparky
The output should be:
&; hello
<; bye
A; sparky
My code is this:
s/keys %hash/values %hash/sg;
but it does not work. It does not recognize any the keys or values
and no changes take place. Does anyone have any suggestions on using
hashes in this kind of situation? I'm looking for the correct
syntax. Any suggestions are appreciated.
Debra
it's equivalent character using a hash. The problem I'm running into
is I am not allowed to use a foreach or while loop to do this (yes,
this is a homework assignment). I must use the -p0777 option with the
#!/usr/bin/perl -p0777 which provides the looping and print function.
I must do this using one line of code only. I must use the /sg
options in the replace statement to satisfy the instructor's
requirements.
The hash table consists of %entity = ( lt=> '<', gt=> '>', amp=> '&',
'#65'=> 'A', quot=> '"', nbs => chr 160);
I need to loop through a text file that contains these &entities of
variable characters and lengths like so:
& hello
< bye
A sparky
The output should be:
&; hello
<; bye
A; sparky
My code is this:
s/keys %hash/values %hash/sg;
but it does not work. It does not recognize any the keys or values
and no changes take place. Does anyone have any suggestions on using
hashes in this kind of situation? I'm looking for the correct
syntax. Any suggestions are appreciated.
Debra