Inline regex

Inline regex

am 16.10.2007 17:18:36 von Steve

Hi all,

I am currently working on a small script that parses text files and
outputs them in a different format, to be used by another program. I am
constantly removing trailing whitespace like so:

$desc = $line[7]; # @line is an array derived from a line in a CSV file
$desc =~ s/\s+$//g; # Trim trailing whitespace

But this is in two lines. How can I copy the variable and remove
whitespace in one line of code?

I know I don't need to, but I tried and failed and now am curious.

Thanks
steve

Re: Inline regex

am 16.10.2007 17:22:48 von it_says_BALLS_on_your forehead

On Oct 16, 11:18 am, steve wrote:
> Hi all,
>
> I am currently working on a small script that parses text files and
> outputs them in a different format, to be used by another program. I am
> constantly removing trailing whitespace like so:
>
> $desc = $line[7]; # @line is an array derived from a line in a CSV file
> $desc =~ s/\s+$//g; # Trim trailing whitespace
>
> But this is in two lines. How can I copy the variable and remove
> whitespace in one line of code?

if you're concerned with brevity of code, you could always use a map.

e.g.

my @trimmed = map { s/\s+$// } @line;

Re: Inline regex

am 16.10.2007 17:26:50 von Abigail

_
steve (steve@nospamtoday.com) wrote on VCLIX September MCMXCIII in
:
}} Hi all,
}}
}} I am currently working on a small script that parses text files and
}} outputs them in a different format, to be used by another program. I am
}} constantly removing trailing whitespace like so:
}}
}} $desc = $line[7]; # @line is an array derived from a line in a CSV file
}} $desc =~ s/\s+$//g; # Trim trailing whitespace
}}
}} But this is in two lines. How can I copy the variable and remove
}} whitespace in one line of code?


I'd leave it as is.

Or, if I have to write it in one line, my first preference would be:

$desc = $line [7]; $desc =~ s/\s+$//; # No need for /g.

Second choice:

($desc = $line [7]) =~ s/\s+$//; # No need for /g.


Abigail
--
perl -wle '$, = " "; sub AUTOLOAD {($AUTOLOAD =~ /::(.*)/) [0];}
print+Just (), another (), Perl (), Hacker ();'

Re: Inline regex

am 16.10.2007 17:30:53 von Uri Guttman

>>>>> "isBoyf" == it says BALLS on your forehead writes:

>> $desc = $line[7]; # @line is an array derived from a line in a CSV file
>> $desc =~ s/\s+$//g; # Trim trailing whitespace
>>
>> But this is in two lines. How can I copy the variable and remove
>> whitespace in one line of code?

isBoyf> if you're concerned with brevity of code, you could always use a map.

isBoyf> my @trimmed = map { s/\s+$// } @line;

hmm, have you tested that? i don't think you will like the results.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org

Re: Inline regex

am 16.10.2007 17:37:01 von Christian Winter

steve wrote:
> I am currently working on a small script that parses text files and
> outputs them in a different format, to be used by another program. I am
> constantly removing trailing whitespace like so:
>
> $desc = $line[7]; # @line is an array derived from a line in a CSV file
> $desc =~ s/\s+$//g; # Trim trailing whitespace
>
> But this is in two lines. How can I copy the variable and remove
> whitespace in one line of code?
>
> I know I don't need to, but I tried and failed and now am curious.

If you want to do the assignment and replacement in one line, you'll
just have to consider operator precedence as diplayed in "perldoc
perlop". The regex comparison operator "=~" binds tighter than
the simple assignment "=", therefore a simple

my $desc = $line[7] =~ s/\s+$//;

won't do that and rather assign the result of the regex replacement
to $desc (btw., no need for the /g modifier there, you already take
care of multiple spaces with the greedy + multiplier). However, simple
brackets suffice to tell perl the order of processing you want:

(my $desc = $line[7]) =~ s/\s+$//;

That's about as short as one can make it.

-Chris

Re: Inline regex

am 16.10.2007 17:51:55 von Steve

Christian Winter wrote:
> steve wrote:
>> I am currently working on a small script that parses text files and
>> outputs them in a different format, to be used by another program. I
>> am constantly removing trailing whitespace like so:
>>
>> $desc = $line[7]; # @line is an array derived from a line in a CSV
>> file
>> $desc =~ s/\s+$//g; # Trim trailing whitespace
>>
>> But this is in two lines. How can I copy the variable and remove
>> whitespace in one line of code?
>>
>> I know I don't need to, but I tried and failed and now am curious.
>
> If you want to do the assignment and replacement in one line, you'll
> just have to consider operator precedence as diplayed in "perldoc
> perlop". The regex comparison operator "=~" binds tighter than
> the simple assignment "=", therefore a simple
>
> my $desc = $line[7] =~ s/\s+$//;
>
> won't do that and rather assign the result of the regex replacement
> to $desc (btw., no need for the /g modifier there, you already take
> care of multiple spaces with the greedy + multiplier). However, simple
> brackets suffice to tell perl the order of processing you want:
>
> (my $desc = $line[7]) =~ s/\s+$//;
>
> That's about as short as one can make it.
>
> -Chris

Wow, thanks for the quick response!

Would the code you suggested remove the whitespace from $line[7] as well?

steve

Re: Inline regex

am 16.10.2007 18:15:36 von glex_no-spam

steve wrote:
[...]
> Wow, thanks for the quick response!
>
> Would the code you suggested remove the whitespace from $line[7] as well?

ahhhh. Why not try it and see for yourself?

Re: Inline regex

am 16.10.2007 18:17:50 von Steve

Christian Winter wrote:
> steve wrote:
>> I am currently working on a small script that parses text files and
>> outputs them in a different format, to be used by another program. I
>> am constantly removing trailing whitespace like so:
>>
>> $desc = $line[7]; # @line is an array derived from a line in a CSV
>> file
>> $desc =~ s/\s+$//g; # Trim trailing whitespace
>>
>> But this is in two lines. How can I copy the variable and remove
>> whitespace in one line of code?
>>
>> I know I don't need to, but I tried and failed and now am curious.
>
> If you want to do the assignment and replacement in one line, you'll
> just have to consider operator precedence as diplayed in "perldoc
> perlop". The regex comparison operator "=~" binds tighter than
> the simple assignment "=", therefore a simple
>
> my $desc = $line[7] =~ s/\s+$//;
>
> won't do that and rather assign the result of the regex replacement
> to $desc (btw., no need for the /g modifier there, you already take
> care of multiple spaces with the greedy + multiplier). However, simple
> brackets suffice to tell perl the order of processing you want:
>
> (my $desc = $line[7]) =~ s/\s+$//;
>
> That's about as short as one can make it.
>
> -Chris

Wow, thanks for the quick response, that works nicely.

Another quick question...
I have a variable that is padded with leading zeros (e.g. "0001.125"). I
want to be able to remove all leading zeros if the value >= 1 ("1.125"),
but leave one leading zero if the value < 1 (e.g. "0000.125" should
become "0.125" not ".125"). Can I do this with one regex?

Re: Inline regex

am 16.10.2007 18:18:14 von Steve

Christian Winter wrote:
> steve wrote:
>> I am currently working on a small script that parses text files and
>> outputs them in a different format, to be used by another program. I
>> am constantly removing trailing whitespace like so:
>>
>> $desc = $line[7]; # @line is an array derived from a line in a CSV
>> file
>> $desc =~ s/\s+$//g; # Trim trailing whitespace
>>
>> But this is in two lines. How can I copy the variable and remove
>> whitespace in one line of code?
>>
>> I know I don't need to, but I tried and failed and now am curious.
>
> If you want to do the assignment and replacement in one line, you'll
> just have to consider operator precedence as diplayed in "perldoc
> perlop". The regex comparison operator "=~" binds tighter than
> the simple assignment "=", therefore a simple
>
> my $desc = $line[7] =~ s/\s+$//;
>
> won't do that and rather assign the result of the regex replacement
> to $desc (btw., no need for the /g modifier there, you already take
> care of multiple spaces with the greedy + multiplier). However, simple
> brackets suffice to tell perl the order of processing you want:
>
> (my $desc = $line[7]) =~ s/\s+$//;
>
> That's about as short as one can make it.
>
> -Chris

Wow, thanks for the quick response, that works nicely.

Another quick question...
I have a variable that is padded with leading zeros (e.g. "0001.125"). I
want to be able to remove all leading zeros if the value >= 1 ("1.125"),
but leave one leading zero if the value < 1 (e.g. "0000.125" should
become "0.125" not ".125"). Can I do this with one regex?

Re: Inline regex

am 16.10.2007 18:24:48 von Steve

steve wrote:
> Christian Winter wrote:
>> steve wrote:
>>> I am currently working on a small script that parses text files and
>>> outputs them in a different format, to be used by another program. I
>>> am constantly removing trailing whitespace like so:
>>>
>>> $desc = $line[7]; # @line is an array derived from a line in a CSV
>>> file
>>> $desc =~ s/\s+$//g; # Trim trailing whitespace
>>>
>>> But this is in two lines. How can I copy the variable and remove
>>> whitespace in one line of code?
>>>
>>> I know I don't need to, but I tried and failed and now am curious.
>>
>> If you want to do the assignment and replacement in one line, you'll
>> just have to consider operator precedence as diplayed in "perldoc
>> perlop". The regex comparison operator "=~" binds tighter than
>> the simple assignment "=", therefore a simple
>>
>> my $desc = $line[7] =~ s/\s+$//;
>>
>> won't do that and rather assign the result of the regex replacement
>> to $desc (btw., no need for the /g modifier there, you already take
>> care of multiple spaces with the greedy + multiplier). However, simple
>> brackets suffice to tell perl the order of processing you want:
>>
>> (my $desc = $line[7]) =~ s/\s+$//;
>>
>> That's about as short as one can make it.
>>
>> -Chris
>
> Wow, thanks for the quick response, that works nicely.
>
> Another quick question...
> I have a variable that is padded with leading zeros (e.g. "0001.125"). I
> want to be able to remove all leading zeros if the value >= 1 ("1.125"),
> but leave one leading zero if the value < 1 (e.g. "0000.125" should
> become "0.125" not ".125"). Can I do this with one regex?

Apologies for the duplicate posts, thunderbird is being a benny...

Re: Inline regex

am 16.10.2007 18:30:54 von Paul Lalli

On Oct 16, 12:17 pm, steve wrote:
> I have a variable that is padded with leading zeros
> (e.g. "0001.125"). I want to be able to remove all leading zeros
> if the value >= 1 ("1.125"), but leave one leading zero if the
> value < 1 (e.g. "0000.125" should become "0.125" not ".125"). Can
> I do this with one regex?

You can, with look-aheads:
$x =~ s/^0+(?!\.)//;

But depending on your data, sprintf() might be simpler.

$x = sprintf('%.3f', $x);

Or, just tell Perl that it really is a number, which might be simpler
still:

$x += 0;

Paul Lalli

Re: Inline regex

am 16.10.2007 18:51:36 von it_says_BALLS_on_your forehead

On Oct 16, 11:30 am, Uri Guttman wrote:
> >>>>> "isBoyf" == it says BALLS on your forehead writes:
>
> >> $desc = $line[7]; # @line is an array derived from a line in a CSV file
> >> $desc =~ s/\s+$//g; # Trim trailing whitespace
> >>
> >> But this is in two lines. How can I copy the variable and remove
> >> whitespace in one line of code?
>
> isBoyf> if you're concerned with brevity of code, you could always use a map.
>
> isBoyf> my @trimmed = map { s/\s+$// } @line;
>
> hmm, have you tested that? i don't think you will like the results.

stupid mistake :-(.

my @trimmed = map { s/\s+$//; $_ } @line;

Re: Inline regex

am 16.10.2007 19:14:06 von nobull67

On Oct 16, 4:26 pm, Abigail wrote:

> Or, if I have to write it in one line, my first preference would be:
>
> $desc = $line [7]; $desc =~ s/\s+$//; # No need for /g.
>
> Second choice:
>
> ($desc = $line [7]) =~ s/\s+$//; # No need for /g.
>

If you really don't like the above format I have some syntactic sugar
for you.

use List::MoreUtils qw ( apply );
my $desc = apply { s/\s+$// } $line[7];

Re: Inline regex

am 16.10.2007 19:14:57 von nobull67

On Oct 16, 5:51 pm, it_says_BALLS_on_your forehead
wrote:
> On Oct 16, 11:30 am, Uri Guttman wrote:

> isBoyf> my @trimmed = map { s/\s+$// } @line;
>
> > hmm, have you tested that? i don't think you will like the results.
>
> stupid mistake :-(.
>
> my @trimmed = map { s/\s+$//; $_ } @line;

That still modifies @line.

Re: Inline regex

am 16.10.2007 19:28:34 von it_says_BALLS_on_your forehead

On Oct 16, 1:14 pm, Brian McCauley wrote:
> On Oct 16, 5:51 pm, it_says_BALLS_on_your forehead
>
> wrote:
> > On Oct 16, 11:30 am, Uri Guttman wrote:
> > isBoyf> my @trimmed = map { s/\s+$// } @line;
>
> > > hmm, have you tested that? i don't think you will like the results.
>
> > stupid mistake :-(.
>
> > my @trimmed = map { s/\s+$//; $_ } @line;

good grief! :-)

my @trimmed = map { my $val = $_; $val =~ s/\s+$//; $val } @line;

Re: Inline regex

am 16.10.2007 19:32:21 von it_says_BALLS_on_your forehead

On Oct 16, 1:28 pm, it_says_BALLS_on_your forehead
wrote:
> On Oct 16, 1:14 pm, Brian McCauley wrote:
>
> > On Oct 16, 5:51 pm, it_says_BALLS_on_your forehead
>
> > wrote:
> > > On Oct 16, 11:30 am, Uri Guttman wrote:
> > > isBoyf> my @trimmed = map { s/\s+$// } @line;
>
> > > > hmm, have you tested that? i don't think you will like the results.
>
> > > stupid mistake :-(.
>
> > > my @trimmed = map { s/\s+$//; $_ } @line;
>
> good grief! :-)
>
> my @trimmed = map { my $val = $_; $val =~ s/\s+$//; $val } @line;

OR, if you WANT to change the original @line:

map { s/\s+$// } @line;

(not sure, but i believe that map in void context is fine in >= 5.8.7,
correct?)

Re: Inline regex

am 16.10.2007 19:49:46 von Christian Winter

it_says_BALLS_on_your forehead schrieb:
>
> OR, if you WANT to change the original @line:
>
> map { s/\s+$// } @line;
>
> (not sure, but i believe that map in void context is fine in >= 5.8.7,
> correct?)

I tend to shun a map without use of its return values like
the devil the holy water, as the same can be accomplished
with a for loop, and the intention is clearer to whoever
gets to maintain my code.

s/\s+$// for @line;

-Chris

Re: Inline regex

am 16.10.2007 20:12:30 von Mirco Wahab

Christian Winter wrote:
> I tend to shun a map without use of its return values like
> the devil the holy water, as the same can be accomplished
> with a for loop, and the intention is clearer to whoever
> gets to maintain my code.
>
> s/\s+$// for @line;

Where the 'map' would (imho) make sense:

...
my @trimmed = map /\s+$/ ? substr($_,0,$-[0]) : $_, @line;
...



(anybody interested in banchmarking the
non-modifying varieties ;-)



Regards

Mirco

Re: Inline regex

am 16.10.2007 21:40:31 von Sherm Pendley

steve writes:

> I am currently working on a small script that parses text files and
> outputs them in a different format, to be used by another program. I
> am constantly removing trailing whitespace like so:
>
> $desc = $line[7]; # @line is an array derived from a line in a CSV file
> $desc =~ s/\s+$//g; # Trim trailing whitespace
>
> But this is in two lines. How can I copy the variable and remove
> whitespace in one line of code?

Why not simply chomp() the line before splitting it into fields?

sherm--

--
Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net

Re: Inline regex

am 16.10.2007 22:12:59 von Uri Guttman

>>>>> "MW" == Mirco Wahab writes:

MW> Where the 'map' would (imho) make sense:

MW> ...
MW> my @trimmed = map /\s+$/ ? substr($_,0,$-[0]) : $_, @line;
MW> ...

why the extra code? just grab the text before the trailing spaces (untested):

my @trimmed = map /^(.*?)\s*$/, @line;

note the use of * in both parts as either can be empty. dunno if this
will run fast or slow due to the double use of *. but as i said in
another thread, trimming white space is so trivial in perl so why waste
effort on doing it in different ways.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org

Re: Inline regex

am 17.10.2007 00:11:51 von Abigail

_
steve (steve@nospamtoday.com) wrote on VCLIX September MCMXCIII in
:
##
## Another quick question...
## I have a variable that is padded with leading zeros (e.g. "0001.125"). I
## want to be able to remove all leading zeros if the value >= 1 ("1.125"),
## but leave one leading zero if the value < 1 (e.g. "0000.125" should
## become "0.125" not ".125"). Can I do this with one regex?


Yes.


What have you tried so far?


Abigail
--
perl -wle 'print "Prime" if (1 x shift) !~ /^1?$|^(11+?)\1+$/'

Re: Inline regex

am 17.10.2007 00:14:33 von Abigail

_
Brian McCauley (nobull67@gmail.com) wrote on VCLIX September MCMXCIII in
:
-: On Oct 16, 4:26 pm, Abigail wrote:
-:
-: > Or, if I have to write it in one line, my first preference would be:
-: >
-: > $desc = $line [7]; $desc =~ s/\s+$//; # No need for /g.
-: >
-: > Second choice:
-: >
-: > ($desc = $line [7]) =~ s/\s+$//; # No need for /g.
-: >
-:
-: If you really don't like the above format I have some syntactic sugar
-: for you.
-:
-: use List::MoreUtils qw ( apply );
-: my $desc = apply { s/\s+$// } $line[7];


That's two lines!

my ($desc) = map {local $_ = $_; s/\s+$//; $_} $line [7];


Abigail
--
perl -wle'print"Êõóô áîïôèåò Ðåòì Èáãëåò"^"\x80"x24'

Re: Inline regex

am 17.10.2007 00:17:24 von Michele Dondi

On Tue, 16 Oct 2007 15:22:48 -0000, it_says_BALLS_on_your forehead
wrote:

>my @trimmed = map { s/\s+$// } @line;

Nope!

my @trimmed map { (my $t=$_) =~ s/\s+$//; $t } @line;

We don't have the subst() method yet.


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Re: Inline regex

am 17.10.2007 00:19:32 von Michele Dondi

On Tue, 16 Oct 2007 16:51:36 -0000, it_says_BALLS_on_your forehead
wrote:

>stupid mistake :-(.
>
>my @trimmed = map { s/\s+$//; $_ } @line;

Stupid Mistake II the Revenge: you will modify @line too, which may be
what you want or not, but generally isn't. In that case you would just

s/\s+$// for @line;


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Re: Inline regex

am 17.10.2007 00:20:13 von Abigail

_
Christian Winter (thepoet_nospam@arcor.de) wrote on VCLIX September
MCMXCIII in :
&& it_says_BALLS_on_your forehead schrieb:
&& >
&& > OR, if you WANT to change the original @line:
&& >
&& > map { s/\s+$// } @line;
&& >
&& > (not sure, but i believe that map in void context is fine in >= 5.8.7,
&& > correct?)
&&
&& I tend to shun a map without use of its return values like
&& the devil the holy water, as the same can be accomplished
&& with a for loop,

What a Pythonesque argument!

Considering anything you can do with 'for' can be done with 'while'
as well, and anything you can do with 'while' can be done with 'for',
which of the two do you stay away from?

Of course, if you use both 'for' and 'while' I don't think this argument
carries any significant weight.

&& and the intention is clearer to whoever
&& gets to maintain my code.

That's in the eye of the beholder.

&& s/\s+$// for @line;

If the intention is write clear code for "whoever gets to maintain my code",
using the aliasing behaviour of 'for' in combination with the implicite
use of $_ twice, wouldn't be high on my list.



Abigail
--
BEGIN {$^H {q} = sub {pop and pop and print pop}; $^H = 2**4.2**12}
"Just "; "another "; "Perl "; "Hacker\n";

Re: Inline regex

am 17.10.2007 00:20:36 von Michele Dondi

On Tue, 16 Oct 2007 17:32:21 -0000, it_says_BALLS_on_your forehead
wrote:

>> my @trimmed = map { my $val = $_; $val =~ s/\s+$//; $val } @line;
>
>OR, if you WANT to change the original @line:
>
>map { s/\s+$// } @line;
>
>(not sure, but i believe that map in void context is fine in >= 5.8.7,
>correct?)

It is fine but considered bad, for good reasons. You would use C
in that case, since that's exactly what it's... for!


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Re: Inline regex

am 17.10.2007 00:23:49 von Michele Dondi

On Tue, 16 Oct 2007 16:17:50 GMT, steve wrote:

>Wow, thanks for the quick response, that works nicely.
>
>Another quick question...

Then you'd probably better start a new thread...


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Re: Inline regex

am 17.10.2007 01:16:46 von Michele Dondi

On 16 Oct 2007 22:14:33 GMT, Abigail wrote:

>That's two lines!
>
> my ($desc) = map {local $_ = $_; s/\s+$//; $_} $line [7];

I often use C myself, but I'm somewhat wary about it since
clpmisc informed me that it can be buggy with tied stuff, althoug I
doubt that $line [7] will contain any.


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Re: Inline regex

am 17.10.2007 01:49:26 von Gunnar Hjalmarsson

Michele Dondi wrote:
> On 16 Oct 2007 22:14:33 GMT, Abigail wrote:
>
>> That's two lines!
>>
>> my ($desc) = map {local $_ = $_; s/\s+$//; $_} $line [7];
>
> I often use C myself, but I'm somewhat wary about it since
> clpmisc informed me that it can be buggy with tied stuff,

Isn't that why it's better written

my ($desc) = map { local *_ = \$_; s/\s+$//; $_ } $line[7];
-----------------------------^----^

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Re: Inline regex

am 17.10.2007 02:36:37 von Tad McClellan

steve wrote:
> Christian Winter wrote:
>> steve wrote:


>>> am constantly removing trailing whitespace like so:

>> (my $desc = $line[7]) =~ s/\s+$//;


> Would the code you suggested remove the whitespace from $line[7] as well?


What happened when you tried it?


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

Re: Inline regex

am 17.10.2007 02:36:37 von Tad McClellan

steve wrote:


> Another quick question...


Then you should start a new thread.


> I have a variable that is padded with leading zeros (e.g. "0001.125"). I
> want to be able to remove all leading zeros if the value >= 1 ("1.125"),
> but leave one leading zero if the value < 1 (e.g. "0000.125" should
> become "0.125" not ".125"). Can I do this with one regex?


$num =~ s/^0+(\d)/$1/;


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

Re: Inline regex

am 17.10.2007 13:05:17 von Abigail

_
Michele Dondi (bik.mido@tiscalinet.it) wrote on VCLIX September MCMXCIII
in :
<> On 16 Oct 2007 22:14:33 GMT, Abigail wrote:
<>
<> >That's two lines!
<> >
<> > my ($desc) = map {local $_ = $_; s/\s+$//; $_} $line [7];
<>
<> I often use C myself, but I'm somewhat wary about it since
<> clpmisc informed me that it can be buggy with tied stuff, althoug I
<> doubt that $line [7] will contain any.


If $line [7] is tied, then you wouldn't know whether s/\s+$// actually
removes trailing white space.

General questions are usually not answerable if you have to account for
all possibilities of tiedness and overloading.

IMO, it's up to the implementor of the tie or overload methods to figure
out how "normal things" should be handled - the user of the normal things
shouldn't have to wonder whether something is tied or overloaded. (That
would defeat the purpose of tie and overload).


Abigail
--
BEGIN {$^H {q} = sub {$_ [1] =~ y/S-ZA-IK-O/q-tc-fe-m/d; $_ [1]}; $^H = 0x28100}
print "Just another PYTHON hacker\n";

Re: Inline regex

am 17.10.2007 13:39:07 von Mirco Wahab

Uri Guttman wrote:
>>>>>> "MW" == Mirco Wahab writes:
> MW> Where the 'map' would (imho) make sense:
> MW> ...
> MW> my @trimmed = map /\s+$/ ? substr($_,0,$-[0]) : $_, @line;
> MW> ...
>
> why the extra code? just grab the text before the trailing spaces (untested):

This was made for "speedy design". I had "a million" of "big strings"
in mind - and tried to provide a practical, nondestructive version
that avoids unnecessary copying (see my last remark in my posting).

> my @trimmed = map /^(.*?)\s*$/, @line;
>
> note the use of * in both parts as either can be empty. dunno if this
> will run fast or slow due to the double use of *. but as i said in
> another thread, trimming white space is so trivial in perl so why waste
> effort on doing it in different ways.

If you have a production system that runs often, you may
optimize at one point because it runs better
everytime then ;-)

(The best "destructive" solution would possibly involve a
direct shortening of the SV* after /\s+$/ without copying
anything around.)

Regards

Mirco

Re: Inline regex

am 17.10.2007 18:06:48 von Uri Guttman

>>>>> "MW" == Mirco Wahab writes:

MW> Uri Guttman wrote:
>>>>>>> "MW" == Mirco Wahab writes:
MW> Where the 'map' would (imho) make sense:
MW> ...
MW> my @trimmed = map /\s+$/ ? substr($_,0,$-[0]) : $_, @line;
MW> ...
>> why the extra code? just grab the text before the trailing spaces
>> (untested):

MW> This was made for "speedy design". I had "a million" of "big strings"
MW> in mind - and tried to provide a practical, nondestructive version
MW> that avoids unnecessary copying (see my last remark in my posting).

first rule of perl optimization is don't guess which is faster,
benchmark it. my experience says that running more perl code such as the
condition expression is slower than staying inside c in one m// op. but
there are many counterexamples to that (the famous trim front and back
code). the overhead for the copy may be less than the work done in the
extra perl code. also the length of the strings and how much is copied
will affect things. i just was showing a way do it with less perl code
on the good chance that it would be faster (definitely cleaner code IMO
too). and the idiom of mapping over list with a grabbing regex is a good
one to show and teach.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org

Re: Inline regex

am 17.10.2007 23:43:33 von nobull67

On Oct 17, 12:05 pm, Abigail wrote:
> _
> Michele Dondi (bik.m...@tiscalinet.it) wrote on VCLIX September MCMXCIII
> in :
> <> On 16 Oct 2007 22:14:33 GMT, Abigail wrote:
> <>
> <> >That's two lines!
> <> >
> <> > my ($desc) = map {local $_ = $_; s/\s+$//; $_} $line [7];
> <>
> <> I often use C myself, but I'm somewhat wary about it since
> <> clpmisc informed me that it can be buggy with tied stuff, althoug I
> <> doubt that $line [7] will contain any.
>
> If $line [7] is tied, then you wouldn't know whether s/\s+$// actually
> removes trailing white space.

> General questions are usually not answerable if you have to account for
> all possibilities of tiedness and overloading.

> IMO, it's up to the implementor of the tie or overload methods to figure
> out how "normal things" should be handled - the user of the normal things
> shouldn't have to wonder whether something is tied or overloaded. (That
> would defeat the purpose of tie and overload).

All that is true but not not the point Michele was making.

local($_) does something different if @line is tied array from what it
does if @line is not tied. There is _nothing_ that the implementor of
the class tied(@line) can do about this.

http://groups.google.com/group/comp.lang.perl.misc/browse_fr m/thread/85eb2aa2c9688c56/4ade11e9618f7b14

Re: Inline regex

am 17.10.2007 23:46:15 von nobull67

On Oct 17, 12:49 am, Gunnar Hjalmarsson wrote:
> Michele Dondi wrote:
> > On 16 Oct 2007 22:14:33 GMT, Abigail wrote:
>
> >> my ($desc) = map {local $_ = $_; s/\s+$//; $_} $line [7];
>
> Isn't that why it's better written
>
> my ($desc) = map { local *_ = \$_; s/\s+$//; $_ } $line[7];
> -----------------------------^----^

No 'cos that still modifies $line[7]