I would like to use a string (e.g. read from a file) as a piece of code in my perl program. For example, I read a string (e.g. $str="1..100,171,398..1000") then I would like to print all the numbers in the range represented by the string.
Thanks,
Dave
For that specific case, see How can I expand a string like "1..15,16" into a list of numbers?
In general,
evalis used to execute code in a string. If you have an entire file of code to execute, usedo. But make sure that the file/string are secure before considering this.If in fact the “code” is restricted to something like your example, do not use eval or do.
Update: turns out Number::Range is dog slow for large ranges; for your case you are far better off using – instead of .. in your ranges and using Set::IntSpan. You ask about efficiency: using an array will consume a few megabytes of memory, but that isn’t all that bad. Set::IntSpan does provide iteration methods you could use instead of just generating the array, but I probably wouldn’t bother.