Expression Problem

N

Noname

Hi,
I have input from STDIN like $Input="1-5,6,8,10-15";
which actually needs to be interpreted as an
array=1,2,3,4,5,6,8,10,11,12,13,14,15
how can it be done,
 
A

Anno Siegel

Noname said:
Hi,
I have input from STDIN like $Input="1-5,6,8,10-15";
which actually needs to be interpreted as an
array=1,2,3,4,5,6,8,10,11,12,13,14,15
how can it be done,

map /(\d+)-(\d+)/ ? $1 .. $2 : $_, split /,/, $Input;

Anno
 
P

Paul GABORIT

À (at) Tue, 10 Aug 2004 07:56:38 GMT,
Noname said:
I have input from STDIN like $Input="1-5,6,8,10-15";
which actually needs to be interpreted as an
array=1,2,3,4,5,6,8,10,11,12,13,14,15
how can it be done,

my @array = map {m/(.*)-(.*)/?($1..$2):$_} split ',', $Input;
 
S

Sam Holden

my @array = map { s/-/../; eval } split /,/, $Input;

evaling arbitrary text from STDIN isn't always a great idea. Sometimes
it is - but the in all those cases the user would be told that perl
expressions were expected...

Something like:

my @array = map {/^(\d+)-(\d+)$/?($1..$2):$_} split /,/, $Input;

would be a little safer.
 
T

Tassilo v. Parseval

Also sprach Bernard El-Hagin:
my @array = map { s/-/../; eval } split /,/, $Input;

Heh, cool. The split() isn't necessary, though:

my @array = map { s/-/../g; eval } $Input;

Tassilo
 
G

gnari

Bernard El-Hagin said:
my @array = map { s/-/../; eval } split /,/, $Input;


please do not do this unless you trust the input 100% to
be in the format described.

malicious input (or a mistake) can result in arbitrary code
execution. (think rm -R /)


gnari
 
G

gnari

Bernard El-Hagin said:
That would result in evaling "rm ..R /". ;-)

lots of destructive commands do not include a '-'.
in any case , there is no /g in your code, so this
limitation can easily be avoided.

system("ls -;rm -R /")

gnari
 
N

Noname

Path:
news2.nokia.com!news1.nokia.com!news2.nokia.com.POSTED!not-for-mail
Newsgroups: comp.lang.perl.misc
Subject: Expression Problem
From: Noname <[email protected]>
User-Agent: Xnews/5.04.25
Lines: 5
Message-ID: <Wc%[email protected]>
Date: Tue, 10 Aug 2004 07:56:38 GMT
NNTP-Posting-Host: 172.21.11.70
X-Complaints-To: (e-mail address removed)
X-Trace: news2.nokia.com 1092124598 172.21.11.70 (Tue, 10 Aug 2004
10:56:38 EET DST) NNTP-Posting-Date: Tue, 10 Aug 2004 10:56:38 EET DST
Organization: Nokia
Xref: news1.nokia.com comp.lang.perl.misc:285995

Hi,
I have input from STDIN like $Input="1-5,6,8,10-15";
which actually needs to be interpreted as an
array=1,2,3,4,5,6,8,10,11,12,13,14,15
how can it be done,

I did had a bad experience with "eval" function so I will surely aviod it ,
and use the one without it ,
thanks for your time and information
 
G

gnari

Bernard El-Hagin said:
[...]
lots of destructive commands do not include a '-'.


I think you took my reply too seriously. :)

that was on purpose :)

anyways, the OP seems to have been aware of the
dangers of indiscriminate eval(), so all's well.

gnari
 
5

510046470588-0001

gnari said:
please do not do this unless you trust the input 100% to
be in the format described.

malicious input (or a mistake) can result in arbitrary code
execution. (think rm -R /)


use the Safe module judiciously

Klaus Schilling
 
J

Jay Tilton

: Hi,
: I have input from STDIN like $Input="1-5,6,8,10-15";
: which actually needs to be interpreted as an
: array=1,2,3,4,5,6,8,10,11,12,13,14,15
: how can it be done,

use Set::IntSpan;
my @array = Set::IntSpan ->new( $Input ) ->elements;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top