Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Search Replace using Hash -p0777

Reply
Thread Tools

Search Replace using Hash -p0777

 
 
dscastroii@earthlink.net
Guest
Posts: n/a
 
      04-12-2007
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

 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      04-12-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> wrote:

> 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";
-------------------------------


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      04-12-2007
(E-Mail Removed) <(E-Mail Removed)> wrote:
> 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.


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      04-12-2007
>>>>> "PG" == Purl Gurl <(E-Mail Removed)> writes:

>> 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,

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


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

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      04-12-2007
>>>>> "PG" == Purl Gurl <(E-Mail Removed)> writes:

PG> Uri Guttman wrote:
>> Purl Gurl wrote:


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

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      04-12-2007
(E-Mail 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.

--
Affijn, Ruud

"Gewoon is een tijger."

 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      04-12-2007
(E-Mail Removed) wrote:
> 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
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      04-15-2007
(E-Mail Removed) wrote:
> &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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
Replace /n with a XHTML <br /> using string.replace Alun ASP .Net 3 02-18-2008 05:52 AM
replace text in IFRAME using innerHTML.replace(...) possible? mscir Javascript 0 10-11-2005 11:21 PM



Advertisments