Ask a Question related to PERL Beginners, Design and Development.
-
James #1
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
-
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. -
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... -
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... -
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... -
shorten expression
Hi, is there a way to shorten this expression? $host=~/^(\S*)\s*/; $host=$1; thanks! -
Steve #2
Re: shorten code
Kipp, James wrote:
my @rows = map [split], split /\n/, $str;
for (@rows) { $_->[2] =~ tr/%//d }
--
Steve
Steve Guest
-
-
Rob #4
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
-
Paul #5
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
-
James #6
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
-
Rob #7
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
-
James #8
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
-
Rob #9
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
-
James #10
RE: shorten code
>
Clever! but this may be the point where where it may be confusing as you and
Paul mentioned :)
James Guest
-
John #11
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



Reply With Quote

