Re: Turn off $ anchor greedy behavior

2009-04-19 Thread Jenda Krynicky
Date sent:  Fri, 17 Apr 2009 18:26:17 -0700 (PDT)
From:   oryann9 orya...@yahoo.com
Subject:Re: Turn off $ anchor greedy behavior
To: Perl Beginners beginners@perl.org

 
 Perl sucks...go Ruby...I did and I am much happier!

Good for you. I'm not sure what are you doing here then though.

I was forced to use Ruby for some time. No thanks!

Jenda
= je...@krynicky.cz === http://Jenda.Krynicky.cz =
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Turn off $ anchor greedy behavior

2009-04-17 Thread oryann9

Perl sucks...go Ruby...I did and I am much happier!

- Original Message 

From: Michael Alipio daem0n...@yahoo.com
To: Perl Beginners beginners@perl.org; John W. Krahn jwkr...@shaw.ca
Sent: Tuesday, April 14, 2009 10:06:39 AM
Subject: Re: Turn off $ anchor greedy behavior


Aha, found it.. The split returned a list and you've just sliced it. giving 
[-1] means the list will start running through the elements backwards. 


--- On Tue, 4/14/09, Michael Alipio daem0n...@yahoo.com wrote:

 From: Michael Alipio daem0n...@yahoo.com
 Subject: Re: Turn off $ anchor greedy behavior
 To: Perl Beginners beginners@perl.org, John W. Krahn jwkr...@shaw.ca
 Date: Tuesday, April 14, 2009, 10:02 PM
  
  Or use split and return the last field:
  
  $ perl -le'
  my $string = boy, pig, 123, 123:412adbd, d0g,
  lajdlf134_ lkadsf !234,\n;
  my $value = ( split /,\s+/, $string )[ -1 ];
 
 Another mind bogling example... :-)
 I thought I would do:
 
 my @value = ( split /,\s+/, $string );
 print $value[6];
 
 How could your example, have printed the last field using [
 -1 ]?
 Can I also print say, the 3rd field using this trick?
 
 
 
 
 
 
  print $value;
  '
  lajdlf134_ lkadsf !234
  
  
  
  
  
  John
  -- Those people who think they know everything are a
 great
  annoyance to those of us who do.-- Isaac
 Asimov
  
  -- To unsubscribe, e-mail:
 beginners-unsubscr...@perl.org
  For additional commands, e-mail:
 beginners-h...@perl.org
  http://learn.perl.org/
 
 
  
 
 -- 
 To unsubscribe, e-mail: beginners-unsubscr...@perl.org
 For additional commands, e-mail: beginners-h...@perl.org
 http://learn.perl.org/


  

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


  

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Turn off $ anchor greedy behavior

2009-04-17 Thread Chas. Owens
On Fri, Apr 17, 2009 at 21:26, oryann9 orya...@yahoo.com wrote:

 Perl sucks...go Ruby...I did and I am much happier!
snip

I looked at Ruby.  You couldn't pay me to go back to a language
that uses a stupid visual pun for a concatenation operator or
forces me to cast variables into different types.

11 + 5 is 16 not a type error.

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




AW: Turn off $ anchor greedy behavior

2009-04-14 Thread Thomas Bätzler
Michael Alipio daem0n...@yahoo.com asked:
 I have a $string that is separated by , and space;
 
 boy, pig, 123, 123:412adbd, d0g, lajdlf134_ lkadsf !234,
 
 
 Now I want to capture the string(s) between last two commas. It consists
 of anything upto 32 characters. that is, right after d0g,\s+ up to the
 last character before the last comma at the end of the line.

#!/usr/bin/perl -w

use strict;

my $string = 'boy, pig, 123, 123:412adbd, d0g, lajdlf134_ lkadsf !234,';

if( $string =~ m/,\s*([^,]*),[^,]*$/ ){
  print $1\n;
}

__END__

Depending on the input data size it might worthwhile to look at rindex() and 
substr() instead of using a RE.

HTH,
Thomas

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: AW: Turn off $ anchor greedy behavior

2009-04-14 Thread Michael Alipio



 my $string = 'boy, pig, 123, 123:412adbd, d0g,
 lajdlf134_ lkadsf !234,';
 
 if( $string =~ m/,\s*([^,]*),[^,]*$/ ){
   print $1\n;
 }

How could you guys write this so simple? My regexp was twice this long.
the regexp after \s* tells perl to match anything (0 or more) that is not a 
comma, right? how come it did not match pig? pig is also followed by comma, 
right? so pig should be captured by ([^,]*), right? I guess perl really looks 
for a match starting from the end of the line.


The string actually looks like this:

ABCD1:5C, 2009-04-14 13:01:24, 2009-04-14, 5, 23, ABC, , , -1, 187, 0, 1.2.3.4, 
20, lkasd123 as_!23:s @12ff,


My Regexp looks like this:
/\.\s+\d+,\s+\d+,\s+(.*),$/


It matches from the comma at the end of the line up to .4 when you go 
backwards. By going as far as this, I can be assured that perl won't find any 
more match, but the regexp looks ugly.

What is wrong with my version?
I think if in the future, if perl finds a line which is not ending in a pattern 
exactly like my regexp then it will fail, however yours i guess won't.


 
 __END__
 
 Depending on the input data size it might worthwhile to
 look at rindex() and substr() instead of using a RE.
 




 HTH,
 Thomas




--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Turn off $ anchor greedy behavior

2009-04-14 Thread Michael Alipio

Hi,


I have a $string that is separated by , and space;

boy, pig, 123, 123:412adbd, d0g, lajdlf134_ lkadsf !234,


Now I want to capture the string(s) between last two commas. It consists of 
anything upto 32 characters. that is, right after d0g,\s+ up to the last 
character before the last comma at the end of the line.

if I do something like

(my $value) = $_ ~= /,\s+(.*),\s+$/;

$value would start matching from pig because when I used $ and it looked 
back, the first thing it would match is , pig upto the end of the line

I wonder how you could match only the pattern which is nearest to the end of 
the line having used $ anchor.

To get around this, I could split the lines push each comma delimited string 
into an array and finally print the last element which is a lot of work to do.

Is there some sort of turning of greedy behavior of the $ anchor?














  

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




AW: AW: Turn off $ anchor greedy behavior

2009-04-14 Thread Thomas Bätzler
Michael Alipio daem0n...@yahoo.com asked:


  my $string = 'boy, pig, 123, 123:412adbd, d0g,
  lajdlf134_ lkadsf !234,';
 
  if( $string =~ m/,\s*([^,]*),[^,]*$/ ){
print $1\n;
  }
 
 How could you guys write this so simple? My regexp was twice this long.

Lots of practice? ;-)

 the regexp after \s* tells perl to match anything (0 or more) that is not
 a comma, right? how come it did not match pig? pig is also followed by
 comma, right? so pig should be captured by ([^,]*), right? I guess perl
 really looks for a match starting from the end of the line.

That would be cool, right? No, my RE is says, I want a comma and maybe some 
whitespace after it, then I'll capture all of the following stuff that's not a 
comma, then I want another comma and possibly some more stuff that's not a 
comma right before the end of the line.

Perl's RE engine starts out by looking at , pig (capturing pig) but the 
part where I said no further comma before the end of the line prevents it 
from matching, since there's a comma after 123 but before the end of the 
string. For the same reasons it fails to match at 123, 123:... and d0g.

That's why I think that a classic C approach using rindex() and substr() 
might be faster in your case:

#!/usr/bin/perl -w

use strict;

my $string = 'boy, pig, 123, 123:412adbd, d0g, lajdlf134_ lkadsf !234,';

my $rightComma = rindex( $string, ',' ) - 1;
my $leftComma = rindex( $string, ',', $rightComma );

print substr( $string, $leftComma + 2, $rightComma - $leftComma ), \n;

__END__

HTH,
Thomas

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Turn off $ anchor greedy behavior

2009-04-14 Thread John W. Krahn

Michael Alipio wrote:

Hi,


Hello,


Subject: Turn off $ anchor greedy behavior


Anchors are not greedy.  Anchors don't even match characters.



I have a $string that is separated by , and space;

boy, pig, 123, 123:412adbd, d0g, lajdlf134_ lkadsf !234,


Now I want to capture the string(s) between last two commas. It
consists of anything upto 32 characters. that is, right after d0g,\s+
up to the last character before the last comma at the end of the line.

if I do something like

(my $value) = $_ ~= /,\s+(.*),\s+$/;


The modifier * is greedy, but that is not your problem.  Matches start 
searching at the left so ',\s+' will match the first comma-whitespace 
and then '(.*)' will match everthing except newline to the last ',\s+' 
comma-whitespace.  The anchor is superfluous because '.*' is greedy.




$value would start matching from pig because when I used $ and it
looked back, the first thing it would match is , pig upto the end
of the line

I wonder how you could match only the pattern which is nearest to the
end of the line having used $ anchor.

To get around this, I could split the lines push each comma delimited
string into an array and finally print the last element which is a lot
of work to do.

Is there some sort of turning of greedy behavior of the $ anchor?


You could put a greedy match in front of your pattern:

$ perl -le'
my $string = boy, pig, 123, 123:412adbd, d0g, lajdlf134_ lkadsf 
!234,\n;

my ( $value ) = $string =~ /.*,\s+(.*),\s+/;
print $value;
'
lajdlf134_ lkadsf !234


Or use split and return the last field:

$ perl -le'
my $string = boy, pig, 123, 123:412adbd, d0g, lajdlf134_ lkadsf 
!234,\n;

my $value = ( split /,\s+/, $string )[ -1 ];
print $value;
'
lajdlf134_ lkadsf !234





John
--
Those people who think they know everything are a great
annoyance to those of us who do.-- Isaac Asimov

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Turn off $ anchor greedy behavior

2009-04-14 Thread Michael Alipio


 
 Or use split and return the last field:
 
 $ perl -le'
 my $string = boy, pig, 123, 123:412adbd, d0g,
 lajdlf134_ lkadsf !234,\n;
 my $value = ( split /,\s+/, $string )[ -1 ];

Another mind bogling example... :-)
I thought I would do:

my @value = ( split /,\s+/, $string );
print $value[6];

How could your example, have printed the last field using [ -1 ]?
Can I also print say, the 3rd field using this trick?






 print $value;
 '
 lajdlf134_ lkadsf !234
 
 
 
 
 
 John
 -- Those people who think they know everything are a great
 annoyance to those of us who do.-- Isaac Asimov
 
 -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org
 For additional commands, e-mail: beginners-h...@perl.org
 http://learn.perl.org/


  

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Turn off $ anchor greedy behavior

2009-04-14 Thread Michael Alipio

Aha, found it.. The split returned a list and you've just sliced it. giving 
[-1] means the list will start running through the elements backwards. 


--- On Tue, 4/14/09, Michael Alipio daem0n...@yahoo.com wrote:

 From: Michael Alipio daem0n...@yahoo.com
 Subject: Re: Turn off $ anchor greedy behavior
 To: Perl Beginners beginners@perl.org, John W. Krahn jwkr...@shaw.ca
 Date: Tuesday, April 14, 2009, 10:02 PM
  
  Or use split and return the last field:
  
  $ perl -le'
  my $string = boy, pig, 123, 123:412adbd, d0g,
  lajdlf134_ lkadsf !234,\n;
  my $value = ( split /,\s+/, $string )[ -1 ];
 
 Another mind bogling example... :-)
 I thought I would do:
 
 my @value = ( split /,\s+/, $string );
 print $value[6];
 
 How could your example, have printed the last field using [
 -1 ]?
 Can I also print say, the 3rd field using this trick?
 
 
 
 
 
 
  print $value;
  '
  lajdlf134_ lkadsf !234
  
  
  
  
  
  John
  -- Those people who think they know everything are a
 great
  annoyance to those of us who do.-- Isaac
 Asimov
  
  -- To unsubscribe, e-mail:
 beginners-unsubscr...@perl.org
  For additional commands, e-mail:
 beginners-h...@perl.org
  http://learn.perl.org/
 
 
   
 
 -- 
 To unsubscribe, e-mail: beginners-unsubscr...@perl.org
 For additional commands, e-mail: beginners-h...@perl.org
 http://learn.perl.org/


  

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Turn off $ anchor greedy behavior

2009-04-14 Thread Chas. Owens
On Tue, Apr 14, 2009 at 10:02, Michael Alipio daem0n...@yahoo.com wrote:



 Or use split and return the last field:

 $ perl -le'
 my $string = boy, pig, 123, 123:412adbd, d0g,
 lajdlf134_ lkadsf !234,\n;
 my $value = ( split /,\s+/, $string )[ -1 ];

 Another mind bogling example... :-)
 I thought I would do:

 my @value = ( split /,\s+/, $string );
 print $value[6];
snip

That only works if you are certain you want the seventh item.  If you
always want the last item (regardless of how many items are in the
array) you should say either

print $value[$#value];

or

print $value[-1];

Since you don't care about the other values it is probably better to
index into the list than to store to a temporary array.

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/