Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: s/\w/random character/g

by Abigail-II (Bishop)
on Apr 08, 2004 at 22:18 UTC ( #343780=note: print w/ replies, xml ) Need Help??


in reply to s/\w/random character/g

Using Dominus 'identity' trick:

#!/usr/bin/perl use strict; use warnings; no warnings qw /syntax/; tie my %h => 'main'; sub TIEHASH {bless []} sub FETCH {chr (ord ('A') + int rand 26)} while (<DATA>) { s/(\w)/$h{$1}/g; print; } __DATA__ ABCDEF
Abigail


Comment on Re: s/\w/random character/g
Download Code
Re: Re: s/\w/random character/g
by davido (Archbishop) on Apr 09, 2004 at 00:52 UTC
    Neat little trick. I didn't realize you could tie entities to package main, though I don't see why not, and your demonstration shows there's nothing preventing it from being done.

    The  no warnings qw/syntax/; line seems unnecessary. Is there a reason for it?

    I wasn't able to find anything that looked particularly questionable about the code, and running with full warnings it didn't produce any complaints. I'm running Perl version 5.8.2 for Win32. Is there an issue with warnings under other versions? ...just curious.

    Also, out of curiosity, what advantage is there to implementing a tied hash in this situation as opposed to a tied scalar?

    Update: Trying to answer my own question I did re-implement your solution with simple tied scalars, and found no functional difference, so I assume that the choice to tie a hash was either habbit, or based on something that hasn't occurred to me.

    Again, loved the trick.


    Dave

      The no warnings qw/syntax/; line seems unnecessary. Is there a reason for it?
      For this program, that line doesn't serve any purpose. But then, neither do the two lines above - yet you aren't inquiring about them. The first six lines are generated whenever I type ^A-P (two chars), and I don't bother to remove the last line if it happens to occur in a program where it's unnecessary.

      Trying to answer my own question I did re-implement your solution with simple tied scalars, and found no functional difference,
      The fact that the entire word is replaced with identical characters didn't strike you as a functional difference? In this specific case, you could get away with using tied scalars if you also used /e, but if letters had to be replaced with a random letter enclosed in parenthesis, using a tied has would lead to:
      s/(\w)/($h{$1})/g;
      while you would need something like:
      s/(\w)/"($s)"/eg;
      which, while it has less characters, is less obvious, IMO.

      Abigail

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://343780]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (9)
As of 2014-10-25 18:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (147 votes), past polls