Search Replace using Hash -p0777

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:

&amp; hello
&lt; bye
&#65 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
 
T

Tad McClellan

My code is this:
s/keys %hash/values %hash/sg;

but it does not work.


Sure it does.

It does not recognize any the keys or values


That is because there no keys or values or hashes anywhere
in that statement, there are only some percent characters
in a regex and a string, where the percent character is not meta.

and no changes take place.


Yes they do:

-------------------------------
#!/usr/bin/perl
use warnings;
use strict;

my %hash;
$_ = 'some keys %hash are here';
s/keys %hash/values %hash/sg;
print "$_\n";
 
T

Tad McClellan

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).


That is not a problem at all.

The hash table consists of %entity = ( lt=> '<', gt=> '>', amp=> '&',
'#65'=> 'A', quot=> '"', nbs => chr 160);


So the name of the hash is %entity?

I need to loop through a text file that contains these &entities of
variable characters and lengths like so:

&amp; hello
&lt; bye
&#65 sparky

The output should be:
&; hello
<; bye
A; sparky
^
^
^ where did that semicolon come from?

My code is this:
s/keys %hash/values %hash/sg;


Where is %hash defined?

Does anyone have any suggestions on using
hashes in this kind of situation?


It looks like the thingies all start with an ampersand character.

I'm looking for the correct
syntax.


You are looking for the correct semantic.

Any suggestions are appreciated.


Your pattern will need to include an ampersand character
in it somewhere.

You access values from a hash using this syntax: $entity{amp}

The key in a hash access can be a variable in place of the
constant string in my example.

If you use parenthesis in the correct place in your pattern,
then the matched characters will be placed into a variable.
 
U

Uri Guttman

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


PG> while (<DATA>)
^^^^^
PG> while (($key, $value) = each %entity)
^^^^^

as usual moronzilla doesn't address the actual problem but its
delusional view of it. since when is a while loop (actually 2 of them
and neither is needed) not using a while loop?

PG> { $_ =~ s/$key/$value/; }

useless use of $_

PG> print $_;

another useless use of $_

uri
 
U

Uri Guttman

PG> (snipped childish trolling)

PG> Uri! You are such a comical idiot!

and you don't know how to read. the OP said NO LOOPS. you used 2
loops. i call that being a moronzilla.

uri
 
D

Dr.Ruud

(e-mail address removed) schreef:
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:

&amp; hello
&lt; bye
&#65 sparky

The output should be:
&; hello
<; bye
A; sparky

My code is this:
s/keys %hash/values %hash/sg;

I think you need something like

s/ & # ampersand
([^;]+) # capture: non-semicolons
(?=;) # look-ahead: semicolon
/$entity{$1}/xsg;

Read `perldoc perlretut` and `perldoc perlre` for details.
I found no reason for the s-modifier though.
 
J

John W. Krahn

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

Then I guess you can't use the -p option because that uses a while loop.


John
 
J

Joe Smith

&amp; hello
&lt; bye
&#65 sparky

The output should be:
&; hello
<; bye
A; sparky

You need to re-read the problem definition.
Since the semicolon is part of HTML entities, the proper output should be:

& hello
< bye
A sparky

-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top