Expression Problem

Discussion in 'Perl Misc' started by Noname, Aug 10, 2004.

  1. Noname

    Noname Guest

    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,
    Noname, Aug 10, 2004
    #1
    1. Advertising

  2. Noname

    Anno Siegel Guest

    Noname <> wrote in comp.lang.perl.misc:
    > 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
    Anno Siegel, Aug 10, 2004
    #2
    1. Advertising

  3. Noname

    Paul GABORIT Guest

    À (at) Tue, 10 Aug 2004 07:56:38 GMT,
    Noname <> écrivait (wrote):
    > 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;

    --
    Paul Gaborit - <http://www.enstimac.fr/~gaborit/>
    Perl en français - <http://www.enstimac.fr/Perl/>
    Paul GABORIT, Aug 10, 2004
    #3
  4. Noname

    Sam Holden Guest

    On Tue, 10 Aug 2004 10:07:54 +0200,
    Bernard El-Hagin <> wrote:
    > Noname <> wrote:
    >
    >> 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,

    >
    >
    > 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.


    --
    Sam Holden
    Sam Holden, Aug 10, 2004
    #4
  5. Also sprach Bernard El-Hagin:

    > Noname <> wrote:
    >
    >> 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,

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


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

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

    Tassilo
    --
    $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
    pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
    $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
    Tassilo v. Parseval, Aug 10, 2004
    #5
  6. Noname

    gnari Guest

    "Bernard El-Hagin" <> wrote in
    message news:Xns95416783DCA6Delhber1lidotechnet@62.89.127.66...
    > Noname <> wrote:
    >
    > > 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,

    >
    >
    > 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
    gnari, Aug 10, 2004
    #6
  7. Noname

    gnari Guest

    "Bernard El-Hagin" <> wrote in
    message news:Xns954174513BF97elhber1lidotechnet@62.89.127.66...
    > "gnari" <> wrote:
    >
    > > "Bernard El-Hagin" <>
    > > wrote in message
    > > news:Xns95416783DCA6Delhber1lidotechnet@62.89.127.66...
    > >> Noname <> wrote:
    > >>
    > >> > 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,
    > >>
    > >>
    > >> 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 /)

    >
    >
    > 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
    gnari, Aug 10, 2004
    #7
  8. Noname

    Noname Guest

    Paul GABORIT <> wrote in
    news::

    > Path:
    > news2.nokia.com!news1.nokia.com!news2.nokia.com.POSTED!not-for-mail
    > Newsgroups: comp.lang.perl.misc
    > Subject: Expression Problem
    > From: Noname <>
    > User-Agent: Xnews/5.04.25
    > Lines: 5
    > Message-ID: <Wc%Rc.22142$>
    > Date: Tue, 10 Aug 2004 07:56:38 GMT
    > NNTP-Posting-Host: 172.21.11.70
    > X-Complaints-To:
    > 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
    Noname, Aug 10, 2004
    #8
  9. Noname

    gnari Guest

    "Bernard El-Hagin" <> wrote in
    message news:Xns954187E601E46elhber1lidotechnet@62.89.127.66...
    > "gnari" <> wrote:
    >
    > [...]
    >
    > >> >> 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 /)
    > >>
    > >>
    > >> That would result in evaling "rm ..R /". ;-)

    > >
    > > 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
    gnari, Aug 10, 2004
    #9
  10. Noname

    Guest

    "gnari" <> writes:
    >
    > 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
    , Aug 10, 2004
    #10
  11. Noname

    Jay Tilton Guest

    Noname <> wrote:

    : 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;
    Jay Tilton, Aug 11, 2004
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,290
  2. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    846
    Alan Moore
    Dec 2, 2005
  3. GIMME
    Replies:
    3
    Views:
    11,955
    vforvikash
    Dec 29, 2008
  4. snacktime
    Replies:
    13
    Views:
    563
    Dan Sommers
    Feb 22, 2005
  5. Jason luo
    Replies:
    3
    Views:
    540
    Peter Ammon
    Aug 19, 2004
Loading...

Share This Page