Search Replace using Hash -p0777

Discussion in 'Perl Misc' started by dscastroii@earthlink.net, Apr 12, 2007.

  1. Guest

    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
     
    , Apr 12, 2007
    #1
    1. Advertising

  2. <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 12, 2007
    #2
    1. Advertising

  3. <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 12, 2007
    #3
  4. Uri Guttman Guest

    >>>>> "PG" == Purl Gurl <> 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 ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Apr 12, 2007
    #4
  5. Uri Guttman Guest

    >>>>> "PG" == Purl Gurl <> 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 ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Apr 12, 2007
    #5
  6. Dr.Ruud Guest

    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."
     
    Dr.Ruud, Apr 12, 2007
    #6
  7. 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
     
    John W. Krahn, Apr 12, 2007
    #7
  8. Joe Smith Guest

    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
     
    Joe Smith, Apr 16, 2007
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alun
    Replies:
    3
    Views:
    4,660
    Masudur
    Feb 18, 2008
  2. Abby Lee
    Replies:
    5
    Views:
    480
    Abby Lee
    Aug 2, 2004
  3. rp
    Replies:
    1
    Views:
    593
    red floyd
    Nov 10, 2011
  4. Srijayanth Sridhar
    Replies:
    19
    Views:
    674
    David A. Black
    Jul 2, 2008
  5. Dan Jacobson

    #!/usr/bin/perl -p0777

    Dan Jacobson, Aug 25, 2003, in forum: Perl Misc
    Replies:
    2
    Views:
    133
    Tad McClellan
    Aug 27, 2003
Loading...

Share This Page