Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

First, bravo on the code. I really suck at writing recursive subs. I didn't intend to rewrite your code. I was just trying to rid it of global variables. Please don't take offense.

I don't like the idea of using @tab as a global. IMO, it would be better to use it as a private variable instead.

{ my @tab; sub teledecode { my $tocode = $_[0]; my @telecode = (); my $i = 0; foreach my $byte ( split //, $tocode ) { $telecode[$i] = $telear[$byte]; $i++; } @tab = (); return arrangecode( "", 0, @telecode ); } sub arrangecode { my ( $first, $i, @dat ) = @_; foreach my $j ( @{ $dat[$i] } ) { if ( $i < ( @dat - 1 ) ) { @tab = arrangecode( $first . $j, $i + 1, @dat ); } # I thnk it is faster written as # push @tab, $first . $j; else { $tab[@tab] = $first . $j; } } return @tab; } }

Now teledecode() is not using @tab globally, but @telear is still a global. We could suck it into this scope, but it is used to make %telealph. It would be nicer if we could condence teledecode() to one line. What if we use an array slice ...

sub teledecode { @tab = (); return arrangecode( '', 0, @telear[ split //, $_[0] ] ); }

Now we could move this array slice outside the sub.

foreach my $toto (@ARGV) { if ( $toto =~ /-d=(\d+)/ ) { # Perhaps a description of what that slice # is doing would help the code maintainer :) print foreach teledecode( @telear[ split //, $1 ] ); } elsif ( $toto =~ /-s=([a-zA-Z_]+)/ ) { print smstype($1); # typo fixed by b10m (thanks) } else { print telecode($toto); } } { my @tab; sub teledecode { @tab = (); return arrangecode( '', 0, @_ ); } sub arrangecode { my ( $first, $i, @dat ) = @_; foreach my $j ( @{ $dat[$i] } ) { if ( $i < ( @dat - 1 ) ) { @tab = arrangecode( $first . $j, $i + 1, @dat ); } else { push @tab, $first . $j; } } return @tab; } }

We could use a hash slice in place of telecode() as well. Removing the last global variable.

else { # This slice should probably explained. print join '', @telealph{ split //, uc( $toto ) }; }
HTH,
Charles

In reply to Re: play with phone numbers & w perl :) by CharlesClarkson
in thread play with phone numbers & w perl :) by dominix

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-25 20:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found