Re: Reg:Regular expression

2009-04-14 Thread Emen Zhao
>> I need to replace \/user\/cce\/g_tool with the variable $temp where

>> $temp=\/user\ /gowri\/rem2

$string =~ s{\\/user\\/cce\\/g_tool}{$temp}

HTH,

Thanks,
--Emen


AW: if statement being ignored

2009-04-14 Thread Thomas Bätzler
Brian  wrote:
> could someone please help me with this little problem?
> I am trying to include an if statement part way through printing.
> When the program reaches the line   if ($Lang = fr ) { print "
> that line gets ignored and the cgi keeps going to the end.
> 
> thanks
> Brian
> 
> 
> 
> #! c:\perl\bin\perl.exe -T
> use warnings;
> #use strict;

You should always "use strict;" unless you know why and when.

As to your if condition, a single "=" is an assignment, not a comparison.

And in any case, you'll want to use the "eq" operator for string comparisons. 
Otherwise the comparison will be done numerically (0 == 0) and the code will 
execute all of the time.

HTH,
Thomas

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




Reg:Regular expression

2009-04-14 Thread Gowri Chandra Sekhar Barla, TLS, Chennai


Hi all,

Please help in writing regular expression for the following scenario 
 
I need to replace \/user\/cce\/g_tool with the variable $temp where
 
$temp=\/user\ /gowri\/rem2

Thanks and regards,
Gowri

DISCLAIMER:
---

The contents of this e-mail and any attachment(s) are confidential and intended 
for the named recipient(s) only.
It shall not attach any liability on the originator or HCL or its affiliates. 
Any views or opinions presented in 
this email are solely those of the author and may not necessarily reflect the 
opinions of HCL or its affiliates.
Any form of reproduction, dissemination, copying, disclosure, modification, 
distribution and / or publication of 
this message without the prior written consent of the author of this e-mail is 
strictly prohibited. If you have
received this email in error please delete it and notify the sender 
immediately. Before opening any mail and 
attachments please check them for viruses and defect.

---

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




if statement being ignored

2009-04-14 Thread Brian

Hi,
could someone please help me with this little problem?
I am trying to include an if statement part way through printing.
When the program reaches the line   if ($Lang = fr ) { print "
that line gets ignored and the cgi keeps going to the end.

thanks
Brian



#! c:\perl\bin\perl.exe -T
use warnings;
#use strict;


use CGI qw/:all/;

use CGI::Carp qw/fatalsToBrowser/;

use HTMLTMPL;

my $t = HTMLTMPL->new();


my $q = new CGI;
my $val1 = $q->param('language');
my $val2 = $q->param('year');
#my $submit = $q->param('Submit');

chomp($Lang = $val1);
chomp($Year_in = $val2);


if ($Lang <= 0 ) {$Lang = en;}
{
# bunch of calcs that work perfectly #
}


$t->output( CGI::header );

{
print "

\"http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd\">



$Year_in<\/title>

SSL decrypt

2009-04-14 Thread Luis Daniel Lucio Quiroz
Hi Camels,

I wonder to know if there is a perl module that let me to decrypt ssl traffic 
(if I have already private key), like ssldump or tshark does.

Best regards,

LD

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




getSaveFile and warn?

2009-04-14 Thread Thomas H. George
The following bit of code:

use Tk;
use strict;
.
.
.
my $topl = $mw -> Toplevel(-title => 'File Selection');
my $fr1 = $topl -> Frame -> pack;
$outfile = $fr1 -> getSaveFile(-filetypes => $types);

warn "Over Write?";
open OUTFILE, ">$outfile";

works and pops up a warning when an attempt is made to open an existing
file but, of course, also writes the warning to STDERR.  I really just
want the popup caution but I only get it when the warn statement is
included.

Should I just redirect the output for STDERR to oblivion or is there a
neater way to do this?

Tom

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




Re: Match and split on array help with output

2009-04-14 Thread John W. Krahn

John W. Krahn wrote:

p...@highdeck.com wrote:

Hi john,


Hello,


 thanks for the prompt reply.

Ok, here's what I'd like it to do.


  #!/usr/bin/perl
  #
  # Build Initial list and put into array.
  @serverlist =  `/usr/bin/wget -q -O - http://www.amazon.co.uk`;
 
 
   for ($index = 0; $index <= $#serverlist; $index++) {
 
 #replace http with newline, all .com etc should now be in 3rd 
field "/"

 $serverlist[$index] =~ s/http/\n/g;

 print $serverlist[$index];

   }

Now, if I run this as above and pipe it to grep thus

 ./webprog.pl |grep ://|cut -d"/" -f3

Then I get...
  ecx.images-amazon.com
  ecx.images-amazon.com
  g-ecx.images-amazon.com
  g-ecx.images-amazon.com
  g-ecx.images-amazon.com
  g-ecx.images-amazon.com
  www.amazon.com">United States<
  www.amazon.de">Germany<
  www.amazon.fr">France<

BUT, I want to do it all in perl itself,


use LWP::Simple;
use Regexp::Common;

my @hosts = map m[https?://([^/]+)/], get( 'http://www.amazon.co.uk/' ) 
=~ /$RE{URI}/g;


print "$_\n" for @hosts;


That *should* be:

use LWP::Simple;
use Regexp::Common;

my @hosts = map m[https?://([^/]+)], get( 'http://www.amazon.co.uk/' ) 
=~ /$RE{URI}/g;


print "$_\n" for @hosts;






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: reading files c vs perl

2009-04-14 Thread Chas. Owens
On Tue, Apr 14, 2009 at 11:49, Rick  wrote:
> is it true that perl will be just as fast as c for reading files ?
>
> for example
>
> will  below be as fast as if it were written in c?
> I said this because on random posts, I see that perl is optimized to work w/
> text files and it should be as fast as perl
>
> open FILE, $file or die "bad filename: $!";
>  while (my $line = ) {
>         #do something w $line
>  }
snip

Why not test it?
With the file already in the filesystem's read-ahead buffer

cow...@amans:~$ time cat /usr/share/dict/words | perl wc.pl
234936

real0m0.083s
user0m0.062s
sys 0m0.015s
cow...@amans:~$ time cat /usr/share/dict/words | ./wc
234936

real0m0.028s
user0m0.013s
sys 0m0.012s

You can decide whether the .04 seconds is worth the extra time it
takes to write the C code.

#include 

int main (int argc, char** argv) {
char   buf[4096];
size_t i;
size_t bytes;
size_t count = 0;

while (bytes = fread(buf, 1, 4096, stdin))
for (i = 0; i < bytes; i++)
if (buf[i] == '\n')
count++;

printf("%d\n", count);

return 0;
}

#!/usr/bin/perl

use strict;
use warnings;

my $count = 0;

$/ = \4096;
while (<>) {
$count += tr/\n//;
}

print "$count\n";



-- 
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/




Re: Match and split on array help with output

2009-04-14 Thread John W. Krahn

p...@highdeck.com wrote:

Hi john,


Hello,


 thanks for the prompt reply.

Ok, here's what I'd like it to do.


  #!/usr/bin/perl
  #
  # Build Initial list and put into array.
  @serverlist =  `/usr/bin/wget -q -O - http://www.amazon.co.uk`;
 
 
   for ($index = 0; $index <= $#serverlist; $index++) {
 
 #replace http with newline, all .com etc should now be in 3rd field "/"

 $serverlist[$index] =~ s/http/\n/g;

 print $serverlist[$index];

   } 



Now, if I run this as above and pipe it to grep thus

 ./webprog.pl |grep ://|cut -d"/" -f3

Then I get...

  ecx.images-amazon.com

  ecx.images-amazon.com
  g-ecx.images-amazon.com
  g-ecx.images-amazon.com
  g-ecx.images-amazon.com
  g-ecx.images-amazon.com
  www.amazon.com">United States<
  www.amazon.de">Germany<
  www.amazon.fr">France<

BUT, I want to do it all in perl itself,


use LWP::Simple;
use Regexp::Common;

my @hosts = map m[https?://([^/]+)/], get( 'http://www.amazon.co.uk/' ) 
=~ /$RE{URI}/g;


print "$_\n" for @hosts;






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/




reading files c vs perl

2009-04-14 Thread Rick

is it true that perl will be just as fast as c for reading files ?

for example

will  below be as fast as if it were written in c?
I said this because on random posts, I see that perl is optimized to 
work w/ text files and it should be as fast as perl


open FILE, $file or die "bad filename: $!";
 while (my $line = ) {
 #do something w $line
 }

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




Re: output record separator in one liner

2009-04-14 Thread Rick

Chas. Owens wrote:

On Mon, Apr 13, 2009 at 15:39, Rick  wrote:
  

perl -lane' print "$F[0] ", "$F[4]" , " $F[5]";'

Is there anyway to incoporate $\ <- output record separtor to do this
instead of printing out w/ manual spaces beteween the variables?


snip

No, you want to use $, or $".  Since $" is already set to a space, you
should probably use it:

perl -lane 'print "@F[0,4,5]"'

You can get it even shorter using -p:

perl -lape '$_="@F[0,4,5]"'


  
I forgot about putting " " around @F[0,4,5] and therefore it didn't have 
spaces between the items.. thanks!!


--
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  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/




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  wrote:

> From: Michael Alipio 
> Subject: Re: Turn off $ anchor greedy behavior
> To: "Perl Beginners" , "John W. Krahn" 
> 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 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/




could not connect to Mssqlserver

2009-04-14 Thread perl pra
Hi Gurus,

I need to connect to MSSQL SERVER (express edition on remote system) through
a perl script. But I get the following error with the code below can any
body help me in that.

 ERROR

Use of uninitialized value $Win32::ODBC::ErrConn in concatenation (.) or
string
at C:/Perl/lib/Win32/ODBC.pm line 785.
Unable to connect to DSN Databasesysadm:[17] [] "[Microsoft][ODBC SQL Server
Dri
ver][TCP/IP Sockets]SQL Server does not exist or access denied."

 ERROR

## CODE SNIP

my $user="sqlserver";
my $pw="servertest";
my $dsn="Databasesysadm"; # name of the DSN we will be using
my $SERVER="remotemachine";
my $DB="projDB";
my %dsnavail;
my @cols;
my @dbs;
my $table;
my @tables;



die "Unable to query available DSN's".Win32::ODBC::Error(  )."\n"   unless
(%dsnavail = Win32::ODBC::DataSources(  ));

foreach my $key (keys %dsnavail)
{
 print "$key=$dsnavail{$key}\n";
}

if (!defined $dsnavail{$dsn})
{
die "unable to create DSN:".Win32::ODBC::Error(  )."\n"
  unless (Win32::ODBC::ConfigDSN(ODBC_ADD_DSN,  "SQL Server",
("DSN=$dsn",  "DESCRIPTION=DSN for PerlSysAdm",  "SERVER=remotemachine",
"DATABASE=$DB", "NETWORK=DBMSSOCN",)));
}


my $dbh = new Win32::ODBC("DSN=$dsn;UID=$user;PWD=$pw;");

die "Unable to connect to DSN $dsn:".Win32::ODBC::Error(  )."\n"  unless
(defined $dbh);

# find the databases on the server
if (defined $dbh->Sql(q{SELECT name from sysdatabases}))
{
die "Unable to query databases:".Win32::ODBC::Error(  )."\n";
}


$dbh->Close(  );

die "Unable to delete DSN:".Win32::ODBC::Error(  )."\n"unless
(Win32::ODBC::ConfigDSN(ODBC_REMOVE_DSN, "SQL Server","DSN=$dsn"));

 ## CODE SNIP





Thanks,

Siva


Re: Match and split on array help with output

2009-04-14 Thread John W. Krahn

p...@highdeck.com wrote:

Hi all,


Hello,


I have some basic code that I want to pull out the web addresses from web pages.
Would like to keep it as basic as possible for easy reading.

The line to replace http with newline seems to work ok.
however the "match" line doesnt seem to pull out the required lines
and I'm not to sure about the split either.

I think it's this line "if ($serverlist[$index]  =~ /:\/\//)" 
that's not giving me what I want.


If I comment out the program after $serverlist[$index] =~ s/http/\n/g;
and then pipe it to grep eg

./webprog.pl |grep ://|cut -d"/" -f3
I get the desired output, to a point, but I'd like to do it all in perl

Thanks for your time.

  Alan.

#!/usr/bin/perl
#
#
# Build Initial list and put into array.
@serverlist =  `/usr/bin/wget -q -O - http://www.anyserver.com`;


for ($index = 0; $index <= $#serverlist; $index++) {

#replace http with newline, all .com etc should now be in 3rd field "/"
$serverlist[$index] =~ s/http/\n/g;
   
 
#pull out all lines with ://   like "grep"

# as these should contain web addresses.
if ($serverlist[$index]  =~ /:\/\//) # does not seem to do what it should 
{

#print $serverlist[$index];
# pull out 3rd field eg. ://my.server.com/
print ((split/\//, $serverlist[$index])[2]); # like cut -d"/" 
-f3
# should now be  my.server.com
   }

 }


It works for me:

$ /usr/bin/wget -q -O - http://www.anyserver.com | grep '://' | cut 
-d"/" -f3 | wc

 97  931656
$ perl -le' 


my @serverlist =  `/usr/bin/wget -q -O - http://www.anyserver.com`;

for ( my $index = 0; $index <= $#serverlist; $index++ ) {
$serverlist[ $index ] =~ s/http/\n/g;
if ( $serverlist[ $index ]  =~ /:\/\// ) {
print( ( split /\//, $serverlist[ $index ] )[ 2 ] );
}
}
' | wc
 98  941653


Or better as:

$ perl -le'
my @serverlist =  `/usr/bin/wget -q -O - http://www.anyserver.com`;

for ( @serverlist ) {
if ( /:\/\// ) {
print( ( split /\// )[ 2 ] );
}
}
' | wc
 97  931656




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 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/




Match and split on array help with output

2009-04-14 Thread perl
Hi all,

I have some basic code that I want to pull out the web addresses from web pages.
Would like to keep it as basic as possible for easy reading.

The line to replace http with newline seems to work ok.
however the "match" line doesnt seem to pull out the required lines
and I'm not to sure about the split either.

I think it's this line "if ($serverlist[$index]  =~ /:\/\//)" 
that's not giving me what I want.

If I comment out the program after $serverlist[$index] =~ s/http/\n/g;
and then pipe it to grep eg

./webprog.pl |grep ://|cut -d"/" -f3
I get the desired output, to a point, but I'd like to do it all in perl

Thanks for your time.

  Alan.

#!/usr/bin/perl
#
#
# Build Initial list and put into array.
@serverlist =  `/usr/bin/wget -q -O - http://www.anyserver.com`;


for ($index = 0; $index <= $#serverlist; $index++) {

#replace http with newline, all .com etc should now be in 3rd field "/"
$serverlist[$index] =~ s/http/\n/g;
   
 
#pull out all lines with ://   like "grep"
# as these should contain web addresses.
if ($serverlist[$index]  =~ /:\/\//) # does not seem to do what it 
should 
{
#print $serverlist[$index];
# pull out 3rd field eg. ://my.server.com/
print ((split/\//, $serverlist[$index])[2]); # like cut -d"/" 
-f3
# should now be  my.server.com
   }

 }

-- 
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  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: 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/




Re: Text::Unidecode behaves differently on two machines

2009-04-14 Thread Dr.Ruud

Kelly Jones wrote:


I "cpan Text::Unidecode" on 2 machines and then ran this code:

use utf8;
use Text::Unidecode;
print unidecode("\x{5317}\x{4EB0}")."\n";
print unidecode("\xd0\x90\xd0\xbb")."\n";
print unidecode("\xe3\x82\xa2")."\n";

On both machines, the first line correctly prints "Bei Jing", the
author's test case.

Second line: "Al" on one machine (correct), "DD>>" on the other.

Third line: "a" on one machine (correct), "aC/" on the other.

Thoughts?


$ perl -Mstrict -MEncode -MText::Unidecode -wle '
my $s = "\xe3\x82\xa2";
print Text::Unidecode::unidecode($s);
Encode::_utf8_on($s);
print Text::Unidecode::unidecode($s);
'
aC/
a


That is the output you should expect.


Text::Unidecode should not secretly "fix" the utf8-flag, for the same 
reason that "_utf_on" starts with an underscore.


$ perl -MText::Unidecode -wle'print $Text::Unidecode::VERSION'
0.04

--
Ruud

--
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  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/




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/