You're on the right track with the first half. The second half is confusing. You're looping through the attributes and trying to read from the file multiple times (which doesn't work -- you can't stand twice in the same stream). You're trying to execute what appears to be a vi command outside of vi.

Try this instead. We'll build up a regex and process each line one by one:

my $regex = qr( '(' . join('|', map { quotemeta( $_ ) } keys %attributes_hash ) . ')' ); while (<H>) { s/$regex/$attributes_hash{ $1 }/g; print; }

That prints the output, so you'll either have to redirect it to another file internally or externally. It also doesn't handle the case of some attributes being longer than others, or being substrings of others. Throwing a sort before the map will help with that.

Of course, if you're processing HTML files with regular expressions, the odds of false positives are not in your favor. Learning to use a module such as HTML::Parser or HTML::TokeParser would really pay off.


In reply to Re: Hash Arrays by chromatic
in thread Hash Arrays by asheesh

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.