RE: Split function in Perl
> I have a problem with the split function. > > > string > - - - > one "two three" "four five" six seven > > should be split to > - - - > one > two three > four five > six > seven > I seem to recall seeing this a long time ago done in a one-liner using eval. Anyone remember that? ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Split function in Perl
$Bill Luebkert wrote: > Michael Louie Loria wrote: > > >>I'm tesing the Text::ParseWords >> >>I'm new in Perl and I'm a little bit confused with the PATTERNS >>option but I'm learning it. >> >>Is this code good for checking valid date in the format -MM-DD? >>or do you have any other suggestions > > > What's with the ~'s starting each line ? > > >>sub isvaliddate { >>~ my $input = shift; >>~ if ($input =~ m!^((?:19|20)\d\d)[- /.](0[1-9]|1[012])[- >>/.](0[1-9]|[12][0-9]|3[01])$!) { >>~# At this point, $1 holds the year, $2 the month and $3 the day >>of the date entered >>~if ($3 == 31 and ($2 == 4 or $2 == 6 or $2 == 9 or $2 == 11)) { >>~ return 0; # 31st of a month with 30 days >>~} elsif ($3 >= 30 and $2 == 2) { >>~ return 0; # February 30th or 31st >>~} elsif ($2 == 2 and $3 == 29 and not ($1 % 4 == 0 and ($1 % 100 >><> 0 or $1 % 400 == 0))) { >>~ return 0; # February 29th outside a leap year >>~} else { >>~ return 1; # Valid date >>~} >>~ } else { >>~return 0; # Not a date >>~ } >>} >> >>The link for that code is >>http://www.regular-expressions.info/dates.html coz you might not >>understand the code > > > # test a few examples: > > foreach ('2005-02-30', '2005-04-31', '2005-13-30', '2005-02-35', '2005-02-00', > '2005-01-30',) { > > my $bool = isvaliddate ($_);# your code > printf "$_ is%s valid\n", $bool ? '' : ' not'; > > $bool = is_valid_date ($_); # alternative code > printf "$_ is%s valid\n", $bool ? '' : ' not'; > } > exit; > > # Reformatted it and changed <> to != and it seems ok to me : > > sub isvaliddate { > my $input = shift; > > # you could just use substr's and length() here instead of a RE > # for hopefully a little more speed : > > # return 0 if length $input != 10 or substr ($input, 4, 1) ne '-' > # or substr ($input, 7, 1) ne '-'; > # my $year = substr $input, 0, 4; > # my $month = substr $input, 5, 2; > # my $day = substr $input, 8, 2; > # then replace $1, $2 and $3 with $year, $month and $day > > if ($input !~ > m#^((?:19|20)\d\d)[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$#) { > return 0; # Not a date > } > > # $1 = year, $2 = month $3 = day of month > > if ($3 == 31 and ($2 == 4 or $2 == 6 or $2 == 9 or $2 == 11)) { > return 0; # 31st of a month with 30 days > } elsif ($3 >= 30 and $2 == 2) { > return 0; # February 30th or 31st > } elsif ($2 == 2 and $3 == 29 and not ($1 % 4 == 0 and > ($1 % 100 != 0 or $1 % 400 == 0))) {# <> 0 was wrong for != 0 > return 0; # February 29th outside a leap year > } > return 1; # Valid date > > } > > This should work as well (maybe better) - letting the system do most of the > grunt work but is much slower than yours : > > sub is_valid_date { > my $datestr = shift; > require Time::Local; > > my @d = (0, 0, 12, (split /-/, $datestr)[2, 1, 0]); > $d[4]--; $d[5] -= 1900; > eval "Time::Local::timegm ([EMAIL PROTECTED])"; # see if it converts to > epoch ok > return $@ ? 0 : 1;# return 0 if error else 1 > > } > > __END__ > > Sorry for the ~ character. My MUA added it. I have tested the code and is really good but I had to change a line of code elsif ($2 == 2 and $3 == 29 and not ($1 % 4 == 0 and ($1 % 100 <> 0 or $1 % 400 == 0))) to elsif ($2 == 2 and $3 == 29 and ($1 % 4 != 0 and ($1 % 100 == 0 or $1 % 400 != 0))) coz I had errors with the former. I think the culprit was the NOT [ not ($1 % 4 == 0 and ($1 % 100 <> 0 or $1 % 400 == 0) ]. Thanks again Michael Louie Loria __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com signature.asc Description: 3412282408-signature.asc ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Split function in Perl
Michael Louie Loria wrote: > I'm tesing the Text::ParseWords > > I'm new in Perl and I'm a little bit confused with the PATTERNS > option but I'm learning it. > > Is this code good for checking valid date in the format -MM-DD? > or do you have any other suggestions What's with the ~'s starting each line ? > sub isvaliddate { > ~ my $input = shift; > ~ if ($input =~ m!^((?:19|20)\d\d)[- /.](0[1-9]|1[012])[- > /.](0[1-9]|[12][0-9]|3[01])$!) { > ~# At this point, $1 holds the year, $2 the month and $3 the day > of the date entered > ~if ($3 == 31 and ($2 == 4 or $2 == 6 or $2 == 9 or $2 == 11)) { > ~ return 0; # 31st of a month with 30 days > ~} elsif ($3 >= 30 and $2 == 2) { > ~ return 0; # February 30th or 31st > ~} elsif ($2 == 2 and $3 == 29 and not ($1 % 4 == 0 and ($1 % 100 > <> 0 or $1 % 400 == 0))) { > ~ return 0; # February 29th outside a leap year > ~} else { > ~ return 1; # Valid date > ~} > ~ } else { > ~return 0; # Not a date > ~ } > } > > The link for that code is > http://www.regular-expressions.info/dates.html coz you might not > understand the code # test a few examples: foreach ('2005-02-30', '2005-04-31', '2005-13-30', '2005-02-35', '2005-02-00', '2005-01-30',) { my $bool = isvaliddate ($_);# your code printf "$_ is%s valid\n", $bool ? '' : ' not'; $bool = is_valid_date ($_); # alternative code printf "$_ is%s valid\n", $bool ? '' : ' not'; } exit; # Reformatted it and changed <> to != and it seems ok to me : sub isvaliddate { my $input = shift; # you could just use substr's and length() here instead of a RE # for hopefully a little more speed : # return 0 if length $input != 10 or substr ($input, 4, 1) ne '-' # or substr ($input, 7, 1) ne '-'; # my $year = substr $input, 0, 4; # my $month = substr $input, 5, 2; # my $day = substr $input, 8, 2; # then replace $1, $2 and $3 with $year, $month and $day if ($input !~ m#^((?:19|20)\d\d)[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$#) { return 0; # Not a date } # $1 = year, $2 = month $3 = day of month if ($3 == 31 and ($2 == 4 or $2 == 6 or $2 == 9 or $2 == 11)) { return 0; # 31st of a month with 30 days } elsif ($3 >= 30 and $2 == 2) { return 0; # February 30th or 31st } elsif ($2 == 2 and $3 == 29 and not ($1 % 4 == 0 and ($1 % 100 != 0 or $1 % 400 == 0))) { # <> 0 was wrong for != 0 return 0; # February 29th outside a leap year } return 1; # Valid date } This should work as well (maybe better) - letting the system do most of the grunt work but is much slower than yours : sub is_valid_date { my $datestr = shift; require Time::Local; my @d = (0, 0, 12, (split /-/, $datestr)[2, 1, 0]); $d[4]--; $d[5] -= 1900; eval "Time::Local::timegm ([EMAIL PROTECTED])"; # see if it converts to epoch ok return $@ ? 0 : 1; # return 0 if error else 1 } __END__ -- ,-/- __ _ _ $Bill LuebkertMailto:[EMAIL PROTECTED] (_/ / )// // DBE CollectiblesMailto:[EMAIL PROTECTED] / ) /--< o // // Castle of Medieval Myth & Magic http://www.todbe.com/ -/-' /___/_<_http://dbecoll.tripod.com/ (My Perl/Lakers stuff) ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Split function in Perl
-BEGIN PGP SIGNED MESSAGE- Hash: SHA512 Thanks for all the replies. I'm tesing the Text::ParseWords I'm new in Perl and I'm a little bit confused with the PATTERNS option but I'm learning it. Is this code good for checking valid date in the format -MM-DD? or do you have any other suggestions sub isvaliddate { ~ my $input = shift; ~ if ($input =~ m!^((?:19|20)\d\d)[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$!) { ~# At this point, $1 holds the year, $2 the month and $3 the day of the date entered ~if ($3 == 31 and ($2 == 4 or $2 == 6 or $2 == 9 or $2 == 11)) { ~ return 0; # 31st of a month with 30 days ~} elsif ($3 >= 30 and $2 == 2) { ~ return 0; # February 30th or 31st ~} elsif ($2 == 2 and $3 == 29 and not ($1 % 4 == 0 and ($1 % 100 <> 0 or $1 % 400 == 0))) { ~ return 0; # February 29th outside a leap year ~} else { ~ return 1; # Valid date ~} ~ } else { ~return 0; # Not a date ~ } } The link for that code is http://www.regular-expressions.info/dates.html coz you might not understand the code Thanks, Michael Louie Loria -BEGIN PGP SIGNATURE- Comment: Public Key: https://www.biglumber.com/x/web?qs=0x4A256EC8 Comment: Public Key: http://www.lorztech.com/GPG.txt Comment: Yahoo ID: michaellouieloria iQEVAwUBQua0F7XBHi2y3jwfAQoUaAf/dkEy1hqp6dG/tBONFj7EPU7/XP+YqBgL YRXEo64R8CP3yMkUJXTQrKSEUWVeitgF2lim1BRzR1VfvTDLFJn1kh02n7tVgw1z /F9n2y0O9pKbqkCm6BE7zzLiZpfPMS+weycbwUvp6dVUVjOLZ073b2LhAcvfq4UU bTMqRLicxIPFTq9U1HPXCq3rrq3PK/u1CLSNfu/7GXoQ64eXSb+TrPdnNTgGLwEh A9KtGrKgGrOkFyhA8dPNypR1aaRVszWTHTUSjRxivXlfjJOmtY1/iIEAN8pVLOUc 9z35ht6O4o3CIBGUOtDF34r8Y2MYTs9mFSJ/7lcBOYtnZN02dUYHSQ== =3GPb -END PGP SIGNATURE- __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Split function in Perl
> -Original Message- > From: $Bill Luebkert > Sent: Monday, July 25, 2005 11:42 PM > > robert wrote: > > >>s/"([^"]+) ([^"]+)"/$1\000$2/g; > > > > holy cow. can you explain that substitution? my brain just about > > popped. > > It's just replacing the blank/space between two words with a > binary 0 and losing the quotes. yeah... the quote/not-a-quote freaked me out because i thought quotes had to be escaped \" anyhow, very cool... i like it :-) > Lyle didn't like that one cause it didn't handle mult spaces. > > Here's another that handles mult spaces : > > use strict; > use warnings; > > $_ = 'one "two three" "four five six" seven eight'; > > # you could call a sub rather than doing it all in the second half > # of the substitute ($tmp is because you can't modify $1). > > s/"([^"]+ +[^"]+)"/my $tmp = $1; $tmp =~ s! !\000!g; sprintf $tmp/eg; > > my @a = split / +/; > foreach (@a) { > s/\000/ /g; > print "$_\n"; > } > > That should make Lyle happy. > well, it better. ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Split function in Perl
robert wrote: >>s/"([^"]+) ([^"]+)"/$1\000$2/g; > > holy cow. can you explain that substitution? my brain just about > popped. It's just replacing the blank/space between two words with a binary 0 and losing the quotes. Lyle didn't like that one cause it didn't handle mult spaces. Here's another that handles mult spaces : use strict; use warnings; $_ = 'one "two three" "four five six" seven eight'; # you could call a sub rather than doing it all in the second half # of the substitute ($tmp is because you can't modify $1). s/"([^"]+ +[^"]+)"/my $tmp = $1; $tmp =~ s! !\000!g; sprintf $tmp/eg; my @a = split / +/; foreach (@a) { s/\000/ /g; print "$_\n"; } That should make Lyle happy. >>__END__ -- ,-/- __ _ _ $Bill LuebkertMailto:[EMAIL PROTECTED] (_/ / )// // DBE CollectiblesMailto:[EMAIL PROTECTED] / ) /--< o // // Castle of Medieval Myth & Magic http://www.todbe.com/ -/-' /___/_<_http://dbecoll.tripod.com/ (My Perl/Lakers stuff) ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Split function in Perl
> -Original Message- > From: $Bill Luebkert > Sent: Monday, July 25, 2005 9:30 PM > > s/"([^"]+) ([^"]+)"/$1\000$2/g; holy cow. can you explain that substitution? my brain just about popped. > > my @a = split / +/; > > foreach (@a) { > s/\000/ /g; # restore embedded spaces > print "$_\n"; > } > > __END__ > ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Split function in Perl
Michael Louie Loria wrote: > Hello, > > I have a problem with the split function. > > string > - - - > one "two three" "four five" six seven > > should be split to > - - - > one > two three > four five > six > seven > > > string > - - - > one two three four five six seven > > should be split to > - - - > one > two > three > four > five > six > seven > > > the difference is the string enclosed with " " is considered as 1 > string even with spaces. As mentioned - you could find a module, but this should work for your case: use strict; use warnings; $_ = 'one "two three" "four five" six seven'; s/"([^"]+) ([^"]+)"/$1\000$2/g; # replace embedded spaces with nulls my @a = split / +/; foreach (@a) { s/\000/ /g; # restore embedded spaces print "$_\n"; } __END__ -- ,-/- __ _ _ $Bill LuebkertMailto:[EMAIL PROTECTED] (_/ / )// // DBE CollectiblesMailto:[EMAIL PROTECTED] / ) /--< o // // Castle of Medieval Myth & Magic http://www.todbe.com/ -/-' /___/_<_http://dbecoll.tripod.com/ (My Perl/Lakers stuff) ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Split function in Perl
http://search.cpan.org/~nwclark/perl-5.8.7/lib/Text/ParseWords.pm -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of Michael Louie Loria Sent: Monday, July 25, 2005 9:57 PM To: perl-win32-users@listserv.ActiveState.com Subject: Split function in Perl -BEGIN PGP SIGNED MESSAGE- Hash: SHA512 Hello, I have a problem with the split function. string - - - one "two three" "four five" six seven should be split to - - - one two three four five six seven string - - - one two three four five six seven should be split to - - - one two three four five six seven the difference is the string enclosed with " " is considered as 1 string even with spaces. Thanks, Michael Louie Loria -BEGIN PGP SIGNATURE- Comment: Public Key: https://www.biglumber.com/x/web?qs=0x4A256EC8 Comment: Public Key: http://www.lorztech.com/GPG.txt Comment: Yahoo ID: michaellouieloria iQEVAwUBQuZ7drXBHi2y3jwfAQqL6QgAiROSQrYOyuoITOPNsSxdtYT4VLDeEy6u LFGQlEcdX2b4nkcPkmNcOEbt6qlnWHjnhQwODEH34+BjIpgAb/7yrIxmlQRPnmnj /4O4x0YnFa71Gl7jUwythyv3gDeBo12x6GA+SZU/sdNL0IbDGu1qe0aXxEL7dt0I kveNDhglPqihuWmAG6cqb0CatkV9na9Fg/whsfHbwIGPY4fYCSPi7GzXT+M/K0Mi yGslp31ibW4ZVWtDm+v6g8dV4RFiKfSSpk8c65S7i384vU0RdhdPMu6Qww2U4PYa yKdLLZ49XTG7AbMHiF/r6VUMf8rUJ0vE0I83uH1hAGI+x40K2tqiag== =icS0 -END PGP SIGNATURE- Start your day with Yahoo! - make it your home page http://www.yahoo.com/r/hs ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs