Professional Web Applications Themes

A simple way? A Perl way? - PERL Beginners

Hi! I am looking for a simple Perl way to decode the following, which can be any grouping of number 2-8,9,11,18-21 Into 2,3,4,5,6,7,8,9,11,18,19,20,21 Any Ideas? Thanks for Your Time and Help, Jerry...

  1. #1

    Default A simple way? A Perl way?

    Hi!

    I am looking for a simple Perl way to decode the following, which can be any
    grouping of number

    2-8,9,11,18-21

    Into
    2,3,4,5,6,7,8,9,11,18,19,20,21

    Any Ideas?

    Thanks for Your Time and Help,

    Jerry

    Jerry Guest

  2. #2

    Default RE: A simple way? A Perl way?

    From: Jerry Preston <mailto:com> wrote:

    : I am looking for a simple Perl way to decode the
    : following, which can be any grouping of number
    :
    : 2-8,9,11,18-21
    :
    : Into
    : 2,3,4,5,6,7,8,9,11,18,19,20,21
    :
    : Any Ideas?
    :
    : Thanks for Your Time and Help,

    This same question seems to pop up every quarter.
    Is this for a course in perl or something?



    Charles Guest

  3. #3

    Default Re: A simple way? A Perl way?



     


    I just did this, out of curiosity what are you doing with it? My method
    handled all kinds of strings, but here's a starter for you:

    $in = '2-8,9,11,18-21';

    inarray = split(',',$in);
    outarray = ();

    foreach (inarray) {
    if (/^([\d.]+)-([\d.]+)$/) {
    push outarray, $1 .. $2;
    }
    else { push outarray, $_; }
    }

    print join(",", outarray), "\n";



    Good luck. (If this was homework, you owe me!)

    - B


    Bryan Guest

  4. #4

    Default Re: A simple way? A Perl way?

    Jerry Preston wrote: 

    Hello,
     

    $ perl -le' print join ",", map /(\d+)-(\d+)/ ? $1 .. $2 : $_, split /,/,
    "2-8,9,11,18-21"'
    2,3,4,5,6,7,8,9,11,18,19,20,21



    John
    --
    use Perl;
    program
    fulfillment
    John Guest

  5. #5

    Default Re: A simple way? A Perl way?


     
    >
    > $ perl -le' print join ",", map /(\d+)-(\d+)/ ? $1 .. $2 : $_, split /,/,
    > "2-8,9,11,18-21"'
    > 2,3,4,5,6,7,8,9,11,18,19,20,21[/ref]


    Ouch, John, you turned my 9 lines into 1?! Impressive.

    - B


    Bryan Guest

  6. #6

    Default RE: A simple way? A Perl way?

    John,

    VERY VERY NICE!!

    No! this is not for school, but for a script that requires user inputs on
    special work request for work.

    Thanks!

    Jerry

    -----Original Message-----
    From: John W. Krahn [mailto:net]
    Sent: Wednesday, September 01, 2004 1:32 AM
    To: Perl Beginners
    Subject: Re: A simple way? A Perl way?


    Jerry Preston wrote: 

    Hello,
     

    $ perl -le' print join ",", map /(\d+)-(\d+)/ ? $1 .. $2 : $_, split /,/,
    "2-8,9,11,18-21"'
    2,3,4,5,6,7,8,9,11,18,19,20,21



    John
    --
    use Perl;
    program
    fulfillment

    --
    To unsubscribe, e-mail: org
    For additional commands, e-mail: org
    <http://learn.perl.org/> <http://learn.perl.org/first-response>


    Jerry Guest

  7. #7

    Default Re: A simple way? A Perl way?

    Tim Johnson wrote: 

    If you are accepting this string as input (from a user) then using eval is a
    major security risk unless you ensure that the string contains only valid data
    and you should probably test $ to see if an error occurred. And you don't
    need to escape periods in a double quoted string.

    my $input = '2-8,9,11,18-21';

    $input =~ tr/0-9,-//cd; # remove invalid characters from $input

    $input =~ s/-/../g;
    print join ',', eval $input;

    die $ if $; # eval reported error, then die



    John
    --
    use Perl;
    program
    fulfillment
    John Guest

Similar Threads

  1. Simple db - PERL vs MS SQL Server
    By Andy in forum PERL Beginners
    Replies: 1
    Last Post: January 4th, 04:40 AM
  2. perl simple cms
    By David Dorward in forum PERL Miscellaneous
    Replies: 8
    Last Post: September 10th, 07:49 PM
  3. simple perl script
    By Jerry Maguire in forum PERL Miscellaneous
    Replies: 1
    Last Post: August 18th, 02:33 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139