Joshua Colson am Montag, 24. Juli 2006 19:32: > On Mon, 2006-07-24 at 13:13 -0400, Ryan Moszynski wrote: > > thanks, that works, but it won't solve my problem. > > > > I'm writing a program where the user enters a list through the keyboard, > > like: "0..10,33..43,100..111" > > > > I would like to pass this list directly into a foreach function. The > > problem is, when i pass my scalar in, i get: > > ### > > Argument "0..15,33..43,100..111" isn't numeric in array element > > ### > > > > i guess a better question is, can I ?cast? that string into a variable > > that can be recognized by the foreach? > > You could eval() the string, but a better solution is outlined in Dr.Ruud's > message because if you eval() the string that is passed in, you're opening > yourself up to a trivial exploit of your code by the user.
I think it's a good idea anyway to check the user's input before starting to use it; if you don't check it, in the worst case an input format error at the end of the input is detected after already having done a lot of work. Insofar, even an eval is possible to get a real array from the string input: # [...] my $user_input='0.. 10, 33..43,100 ..111'; # input sanitizing # my $re_range=qr/\d+\s*\.\.\s*\d+/; $user_input=~/^\s*$re_range(?:\s*,\s*$re_range)*\s*$/ or die 'invalid input!'; my @list4=eval $user_input; # [...] Dani -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>