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*