[tpm] Array slice using a variable question

Uri Guttman uri at stemsystems.com
Fri Jul 11 14:51:18 PDT 2008


>>>>> "MF" == Mark Fowle <mfowle at navicominc.com> writes:

  MF> my $slice = "1..3";
  MF> my @slice = eval "($slice)";

eval may be simpler and even cleaner but it should be avoided at all
costs. if the input format is exactly like what you show then you can
grab out the numbers and get a range from that.

	my $slice = "1..3";
	my @range = ( $1 .. $2 ) if $slice =~ /(\d+)\.\.(\d+)/ ;

a little more code but much safer and possibly even faster.

in general, avoid eval unless it is the only solution or so much better
than all the other solutions. it is rarely needed and almost never for
basic data munging like this.

uri

-- 
Uri Guttman  ------  uri at stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


More information about the toronto-pm mailing list