Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re^2: substring exists at particular postion?

by particle (Vicar)
on May 23, 2002 at 12:04 UTC ( [id://168746]=note: print w/replies, xml ) Need Help??

in reply to Re: substring exists at particular postion?
in thread substring exists at particular postion?

if you stick with this solution, i'd use elsif instead of else if--it's proper perl. it's already been mentioned to cache the result from substr, so i'll leave that alone.

but i'd be inclined to use a hash rather than elseif. if you use subroutines for the work, it'll be easy to maintain your code when you need to do more than just print to a handle, because all the code's in one place. here's a fully working example:

#!/usr/bin/perl -w use strict; $|++; sub do_something(*@) { local *HANDLE = shift; print HANDLE @_; } # set up filehandles for this example # you'll probably use open()... local *B = *STDOUT; local *T = *STDERR; my %map_char_to_handle = ( B => *B, T => *T, # etc. ); while(<DATA>) { my $test_for_handle = substr($_, 2, 1); defined $map_char_to_handle{$test_for_handle} and do_something($map_char_to_handle{$test_for_handle}, $_); } __DATA__ ABC123 CAB234 BTT456 BXC789

~Particle *accelerates*

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (8)
As of 2024-05-28 08:03 GMT
Find Nodes?
    Voting Booth?

    No recent polls found