Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default shorten code

    Hi
    I want to split the string below into rows and then columns. I know i can do
    a couple of splits, push array refs onto a new array, regex it, etc.. But I
    was know the talented people on this list can shorten the code considerably.
    I want to split at each new line for the row, then split the row into
    columns, and get rid of the "%" on the third column. I can post my
    embarrassingly long code if you like.
    Thanks for any help
    Jim
    ----

    $str = "
    /var 0.99 50%
    /usr 0.58 71%
    /tmp 0.49 1%
    "

    James Guest

  2. Similar Questions and Discussions

    1. SHORTEN GAP BETWEEN LABEL AND CFINPUT
      How do you adjust the gap between a CFINPUT control and it's label? I want to bring the label closer to the input box.
    2. Can I shorten the time for a live screen refresh?
      To see an object as I'm dragging it, I have to hold the mouse button down for a few seconds before dragging it. Is there a registry or other setting...
    3. how can I shorten download time?
      So. Our website is kind of a Demo for our production company. It has some video. It's all pretty small but it adds up. The download time for the...
    4. you *must* be able to shorten this...
      Hello, I've written a short script to read in a form with HTML-style tags (form is postscript, can be 0.5MB to 20MB+, tags are alphanumerical...
    5. shorten expression
      Hi, is there a way to shorten this expression? $host=~/^(\S*)\s*/; $host=$1; thanks!
  3. #2

    Default Re: shorten code

    Kipp, James wrote: 
     

    my @rows = map [split], split /\n/, $str;
    for (@rows) { $_->[2] =~ tr/%//d }

    --
    Steve
    Steve Guest

  4. #3

    Default RE: shorten code


     

    Neat. You saved me 6 lines of code :)
    Thank You !!



    James Guest

  5. #4

    Default Re: shorten code

    James Kipp wrote: 

    Hi James.

    Don't forget that shortening code is usually more of a pastime
    than anything useful, but if your code is /huge/ it can add to
    readability.

    Does this help? The grep just throws out blank lines.

    Cheers,

    Rob


    use strict;
    use warnings;

    my $str = "
    /var 0.99 50%
    /usr 0.58 71%
    /tmp 0.49 1%
    ";

    my @data = map { tr/%//d; [split ' '] } grep /\S/, split "\n", $str;

    use Data::Dumper;
    print Dumper \@data;

    **OUTPUT

    $VAR1 = [
    [
    '/var',
    '0.99',
    '50'
    ],
    [
    '/usr',
    '0.58',
    '71'
    ],
    [
    '/tmp',
    '0.49',
    '1'
    ]
    ];



    Rob Guest

  6. #5

    Default RE: shorten code

    > > my @rows = map [split], split /\n/, $str; 
    >
    > Neat. You saved me 6 lines of code :)
    > Thank You !![/ref]

    Make sure its meaningful to you or someone else maintaining it. I would
    rather see 6 extra lines of code and have it mean something to me at a
    glance then to just write code that is short.

    My 2 cents.

    PK

    Paul Guest

  7. #6

    Default RE: shorten code

     
    > >
    > > Neat. You saved me 6 lines of code :)
    > > Thank You !![/ref]
    >
    > Make sure its meaningful to you or someone else maintaining
    > it. I would
    > rather see 6 extra lines of code and have it mean something to me at a
    > glance then to just write code that is short.
    >[/ref]

    Thanks Paul and I agree with you. I do think Steve's code is still easy to
    follow and with a comment or 2, should be very clear.


    James Guest

  8. #7

    Default Re: shorten code

    Steve Grazzini wrote: 

    >
    > my @rows = map [split], split /\n/, $str;
    > for (@rows) { $_->[2] =~ tr/%//d }[/ref]

    Not quite! Because the string starts and ends with newslines
    this leaves @rows with five elements. Consequently the tr//
    dies when asked to operate on the undefined $_->[2].

    I admit that I thought this was more than likely to have come
    from a file in the first place, but even so the trailing "\n"
    is more than likely.

    Rob


    Rob Guest

  9. #8

    Default RE: shorten code

    > > > $str = " 
    > >
    > > my @rows = map [split], split /\n/, $str;
    > > for (@rows) { $_->[2] =~ tr/%//d }[/ref]
    >
    > Not quite! Because the string starts and ends with newslines
    > this leaves @rows with five elements. Consequently the tr//
    > dies when asked to operate on the undefined $_->[2].
    >
    > I admit that I thought this was more than likely to have come
    > from a file in the first place, but even so the trailing "\n"
    > is more than likely.
    >[/ref]

    Good point. However I must apologize for posting the wrong string. The
    string as it is created does not have beginning and ending new lines, so
    steve's code works.

    James Guest

  10. #9

    Default Re: shorten code

    James Kipp wrote: 
    > >
    > > Not quite! Because the string starts and ends with newslines
    > > this leaves @rows with five elements. Consequently the tr//
    > > dies when asked to operate on the undefined $_->[2].
    > >
    > > I admit that I thought this was more than likely to have come
    > > from a file in the first place, but even so the trailing "\n"
    > > is more than likely.
    > >[/ref]
    >
    > Good point. However I must apologize for posting the wrong string. The
    > string as it is created does not have beginning and ending new lines, so
    > steve's code works.[/ref]

    In which case

    my @data = map { tr/%//d; [split] } split /\n/, $str;

    :)

    Rob


    Rob Guest

  11. #10

    Default RE: shorten code



    Clever! but this may be the point where where it may be confusing as you and
    Paul mentioned :)

    James Guest

  12. #11

    Default Re: shorten code

    James Kipp wrote: 

    Hello,
     


    $ perl -le'
    use Data::Dumper;
    my $str = "
    /var 0.99 50%
    /usr 0.58 71%
    /tmp 0.49 1%
    ";
    print Dumper \$str;
    my @new = map [ split ], split /%\s+/, $str;
    print Dumper \@new;
    '
    $VAR1 = \'
    /var 0.99 50%
    /usr 0.58 71%
    /tmp 0.49 1%
    ';

    $VAR1 = [
    [
    '/var',
    '0.99',
    '50'
    ],
    [
    '/usr',
    '0.58',
    '71'
    ],
    [
    '/tmp',
    '0.49',
    '1'
    ]
    ];



    John
    --
    use Perl;
    program
    fulfillment
    John Guest

Posting Permissions

  • You may not post new threads
  • You may 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