Re: Arrow dereference operator question
On 5/31/07, David Unric <[EMAIL PROTECTED]> wrote: Based on perlref documentation arrow operator between brackets subscripts may be omitted so the following code is valid: @array = ( [1, 2], [3, 4] ); $element = $array[0][0];# shorthand for $element = $array[0]->[0] Could somebody explain why it causes syntax error when the above rule is applied to returned value of a subroutine ? sub mysub { @array = ( [1, 2], [3, 4] ); return @array; } $element = (&mysub)[0][0]; # $elem = (&mysub)[0]->[0] is valid -- syntax error at testarr.pl line 7, near "][" My best guess is that the problem here is that (mysub())* is a list not an array. Lists are not multidimensional. Try my $element = ([1, 2], [3, 4])[0][0]; Notice how it gets the same error? The simple solution is to use the arrow, or have the sub return an arrayref. Here are some ways to do it: #!/usr/bin/perl use strict; use warnings; sub list { my @array = ( [1, 2], [3, 4] ); return @array; } sub aref { my @array = ( [1, 2], [3, 4] ); return [EMAIL PROTECTED]; } print ( (list())[0]->[0], "\n", "${[list()]}[0][1]\n", "${aref()}[1][0]\n", (aref())->[1][1], "\n" ); * don't use &mysub unless you know why you are doing it, use mysub() instead -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Outlook CSV Parser
On Wed, 30 May 2007 10:38:40 +0200, "Dr.Ruud" <[EMAIL PROTECTED]> wrote: > You forgot to supply a link to such a file. Or show a __DATA__ section > for testing. http://download.deeproot.in/~kamathln/outlook-encrtypted-sample.csv -- Cheers, Laxminarayan G Kamath A e-mail: [EMAIL PROTECTED] Work URL: http://deeproot.in -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Outlook CSV Parser
On 05/31/2007 02:32 AM, Laxminarayan G Kamath A wrote: http://download.deeproot.in/~kamathln/outlook-encrtypted-sample.csv Well I asked for it. :-) It's impossible to tell where one record ends and another record begins with that file. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Outlook CSV Parser
Laxminarayan G Kamath A: > Ruud: >> You forgot to supply a link to such a file. Or show a __DATA__ >> section for testing. > > http://download.deeproot.in/~kamathln/outlook-encrtypted-sample.csv OK, lets check how wellformed it is: perl -we' local $/; $_ = <>; s/"[^"]*"//g; s/(?<=,)[^",]+(?=,)//g; s/^[^,"]*,+//; print ' outlook-encrtypted-sample.csv That prints: --- "3snji [EMAIL PROTECTED] --- so AFAICS there is a problem somewhere at the end. Maybe try a zipped version? -- Affijn, Ruud "Gewoon is een tijger." -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Arrow dereference operator question
I did suspected it would have something to do with the difference between array and list contexts. Your analysis seems to be correct. Thank you for solving this puzzle :) Regards On 5/31/07, Chas Owens <[EMAIL PROTECTED]> wrote: On 5/31/07, David Unric <[EMAIL PROTECTED]> wrote: > Based on perlref documentation arrow operator between brackets subscripts > may be omitted so the following code is valid: > > @array = ( [1, 2], [3, 4] ); > $element = $array[0][0];# shorthand for $element = $array[0]->[0] > > > Could somebody explain why it causes syntax error when the above rule is > applied to returned value of a subroutine ? > > sub mysub { > @array = ( [1, 2], [3, 4] ); > > return @array; > } > > $element = (&mysub)[0][0]; # $elem = (&mysub)[0]->[0] is valid > -- > syntax error at testarr.pl line 7, near "][" > My best guess is that the problem here is that (mysub())* is a list not an array. Lists are not multidimensional. Try my $element = ([1, 2], [3, 4])[0][0]; Notice how it gets the same error? The simple solution is to use the arrow, or have the sub return an arrayref. Here are some ways to do it: #!/usr/bin/perl use strict; use warnings; sub list { my @array = ( [1, 2], [3, 4] ); return @array; } sub aref { my @array = ( [1, 2], [3, 4] ); return [EMAIL PROTECTED]; } print ( (list())[0]->[0], "\n", "${[list()]}[0][1]\n", "${aref()}[1][0]\n", (aref())->[1][1], "\n" ); * don't use &mysub unless you know why you are doing it, use mysub() instead
did I get greedy quantifiers wrong ?
I seem to be having some conceptual problem with greedy quantifiers .. My understanding is that it matches as much as follows while still allowing rest of the regex to match. But look at the following example : $str = mississippi; $str =~ m/m(.*i)(.*pi)/; print "one is $1 \n"; print "two is $2 \n"; $str = mississippi; $str =~ m/m(.*i?)(.*pi)/; print "one is $1 \n"; print "two is $2 \n"; In the first code snippet, I expected first regex (.*i) to match till ississip and leave pi for (.*pi) regex. But what I get as the output of this script is : one is ississi two is ppi one is ississip two is pi Why is that perl is leaving ppi to second regex while it can continue till first p in ppi and can still get the second regex to get a match ? Thanks, Sharan -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: zero width lookahead match
> $ perl -wle' > $string = "abc"; > while ($string =~ /(.*?)/g) { > print pos($string), ": ", $1;} > > ' > 0: > 1: a > 1: > 2: b > 2: > 3: c > 3: > Can someone explain the working of the g modifier since my knowledge of using g was to use it for substituting globally... Here i get what paul is trying to explain but if i take out g from the statement why does it say use of uninitialized value and prints nothing. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Passing arguments to subroutine
Hi All, I need to pass the result of prepare statement as an argument to the subroutine. sub abc() { my $self= shift; my($id,$title) = @_; my $sth1= $databasehandle->prepare("select file_path from xyz where id='$id' and title like '$title'"); my $res = $sth1->execute(); my @row = $sth1->fetchrow_array; print @row; &deleteposter_file(@row); } #-Deletes from the File system sub delete_file() { my $self = shift; my $file_path = @_; # extract the file name # my @parts = split('\/',$file_path); # my $file =$parts[$#parts]; # #unlink($file); # if( -e "file" ) { # system("rm $file_path"); unlink($file_path); } abc is calling delete_file() . where it need to delete the file stored at the location mentioned in file_path. Its not giving me an error but its not deleting the files from the location. Any help. Thanks in advance. Alma -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: zero width lookahead match
Its the same logic - continue after first substitution/match. In case of subst.. it continues and in case of regex, the search continues after first match until the complete string is exhausted On 30 May 2007 22:54:39 -0700, jeevs <[EMAIL PROTECTED]> wrote: > $ perl -wle' > $string = "abc"; > while ($string =~ /(.*?)/g) { > print pos($string), ": ", $1;} > > ' > 0: > 1: a > 1: > 2: b > 2: > 3: c > 3: > Can someone explain the working of the g modifier since my knowledge of using g was to use it for substituting globally... Here i get what paul is trying to explain but if i take out g from the statement why does it say use of uninitialized value and prints nothing. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Passing arguments to subroutine
Alma wrote: Hi All, I need to pass the result of prepare statement as an argument to the subroutine. -snip- abc is calling delete_file() . where it need to delete the file stored at the location mentioned in file_path. Its not giving me an error but its not deleting the files from the location. A few thoughts: 1. Where are you setting '&delete_posterfile'; and to what are you setting it? 2. IIRC, following a sub declaration with empty parentheses tells the compiler that that sub doesn't take any parameters. Within the scope of the delete_file sub, @_ is set to '()'; so shifting or assigning from @_ to a list will always result in null assignments. Try removing the '()' from the sub declaration lines. -- Jonathan "Dataweaver" Lang -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: did I get greedy quantifiers wrong ?
On 5/31/07, Sharan Basappa <[EMAIL PROTECTED]> wrote: snip $str =~ m/m(.*i?)(.*pi)/; snip ? only means non-greedy when following a * or a +. When it follows a pattern it means optional. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: did I get greedy quantifiers wrong ?
On 5/31/07, Chas Owens <[EMAIL PROTECTED]> wrote: On 5/31/07, Sharan Basappa <[EMAIL PROTECTED]> wrote: snip > $str =~ m/m(.*i?)(.*pi)/; snip ? only means non-greedy when following a * or a +. When it follows a pattern it means optional. Oh, and it makes the generalized* versions non-greedy as well. *{n}, {n,}, and {n, m} -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: did I get greedy quantifiers wrong ?
Thanks Chas .. I was wondering about the first regex str =~ m/m(.*i)(.*pi)/; did not match all the way till mississip. In fact my initial understanding was that the regex would match mississippi leaving nothing for second regex. Can you throw some light on this .. On 5/31/07, Chas Owens <[EMAIL PROTECTED]> wrote: On 5/31/07, Sharan Basappa <[EMAIL PROTECTED]> wrote: snip > $str =~ m/m(.*i?)(.*pi)/; snip ? only means non-greedy when following a * or a +. When it follows a pattern it means optional. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: did I get greedy quantifiers wrong ?
On May 31, 6:02 am, [EMAIL PROTECTED] (Sharan Basappa) wrote: > I seem to be having some conceptual problem with greedy quantifiers .. > My understanding is that it matches as much as follows while still > allowing rest of the > regex to match. 90% correct. The other 10% is that the match starts left-to-right. It will start with the first part of the string that can match, and match as much of *that* as possible. It will not search the rest of the string to see if a longer match is possible later. For example: $string = 'abbaba'; $string =~ /(b*)/; In this case, $1 will be set to 'bb', because that is the *first* longest string it could find, even though if it had continued, it would have been able to find 'b' later. > But look at the following example : > $str = mississippi; > $str =~ m/m(.*i)(.*pi)/; > print "one is $1 \n"; > print "two is $2 \n"; > > $str = mississippi; > $str =~ m/m(.*i?)(.*pi)/; This doesn't mean what you think it means. This tells Perl that the second token - (.*i?) can match as much of anything as it can, followed by 0 or 1 i's. That ? does not apply to the .* unless you put it right after the *. Compare and contrast with: (.*?i), which means to match as little of anything as possible, followed by exactly one i. > print "one is $1 \n"; > print "two is $2 \n"; > > In the first code snippet, I expected first regex (.*i) to match till > ississip Right there is a problem. Your token is (.*i). That is, the last character of this token must be an i. It can't end with a p. That doesn't match. The .* matches as much as it can until the last 'i', then saves the 'i' for the i in the token. > and leave pi for (.*pi) regex. > > But what I get as the output of this script is : > > one is ississi > two is ppi > one is ississip > two is pi > > Why is that perl is leaving ppi to second regex while it can continue > till first p It can't. The token ends in an i. 'i' must be the last thing that (.*i) matches. Paul Lalli -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Passing arguments to subroutine
On May 31, 4:27 am, [EMAIL PROTECTED] (Alma) wrote: > I need to pass the result of prepare statement as an argument to the > subroutine. > > sub abc() The () there very specifically say "This subroutine takes no arguments". > { > my $self= shift; > my($id,$title) = @_; ... and yet you're retrieving arguments from the subroutine. > my $sth1= $databasehandle->prepare("select file_path from xyz > where id='$id' and title like '$title'"); > my $res = $sth1->execute(); > > my @row = $sth1->fetchrow_array; > print @row; > > &deleteposter_file(@row); The & there specifically says to ignore the prototype (ie, how many arguments the subroutine says it needs). So first you tell your subroutines "don't take any arguments", and then you tell them "ignore what I just told you." Stop doing that. > } > > #-Deletes from the File system > > sub delete_file() Where the heck did this subroutine come from? The subroutine you called above is called deleteposter_file. And you're again telling it it takes no arguments, even though you passed arguments to it. > { > my $self = shift; What? You're not calling this subroutine as a method. Why are you thinking it has an object reference as the first argument? > my $file_path = @_; This sets $file_path equal to the number of arguments left in @_ (after you shifted off $self). It does not set $file_path equal to the first element of @_. For that, you want either: my ($file_path) = @_; or my $file_path = shift; or my $file_path = $_[0]; > # extract the file name > # my @parts = split('\/',$file_path); > # my $file =$parts[$#parts]; > # #unlink($file); Why are you commenting this out? Why are you not checking for errors? Why are you not printing $file to see what it is? unlink $file or die "Cannot unlink '$file': $!"; > # if( -e "file" ) { "file" is not the same thing as $file. even if you change it to $file, you're looking for $file in the current directory, even though you just determined it may not be located in that directory, because you were originally passed a full path. > # system("rm $file_path"); > > unlink($file_path); again, unlink $file_path or die "Cannot unlink '$file_path': $!"; > > } > > abc is calling delete_file() . where it need to delete the file stored > at the location mentioned in file_path. > > Its not giving me an error You didn't ask it to. system calls don't report errors unless you check for an error and print the error out yourself. Paul Lalli -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Passing arguments to subroutine
On 31 May 2007 01:27:26 -0700, Alma <[EMAIL PROTECTED]> wrote: snip &deleteposter_file(@row); snip sub delete_file() snip This would seem to be the problem, also where did you learn that you should put & on the front of your subroutine calls? I am curious because I keep seeing people do it without understanding what it is for. Way back, in the dawn of time, it was the way to call subroutines, but it then it changed to just "deleteposter_file(@row);". from perldoc perlsub To call subroutines: NAME(LIST);# & is optional with parentheses. NAME LIST; # Parentheses optional if predeclared/imported. &NAME(LIST); # Circumvent prototypes. &NAME; # Makes current @_ visible to called subroutine. Note the extra bits that happen when you call it with &? They can be handy when you expect them and trying to achieve a specific effect, but they can cause problems for the unwary: #!/usr/bin/perl use strict; use warnings; sub i_have_a_prototype($$$) { print "I can only be called with three parameters,", "they are $_[0], $_[1], and $_[2]\n" } eval qq{ i_have_a_prototype(1); } or print "[EMAIL PROTECTED]"; eval qq{ i_have_a_prototype(1, 2);} or print "[EMAIL PROTECTED]"; i_have_a_prototype(1, 2, 3); &i_have_a_prototype(6); #oops Also #!/usr/bin/perl use strict; use warnings; sub outer { if ($_[0] eq 'none') { &inner; } else { &inner("arg1", "arg2"); } } #this is not treadsafe, but the first call #sets up foobar, and all subsequent calls #use foobar sub inner { if (@_ == 0) { print "no args, so setup foobar\n"; return; } #if we get here then args were passed #and foobar should setup local $" = ", "; print "I was call like this inner(@_)\n"; } outer('none'); #setup foobar outer('use'); #use foobar -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: did I get greedy quantifiers wrong ?
On 5/31/07, Sharan Basappa <[EMAIL PROTECTED]> wrote: Thanks Chas .. I was wondering about the first regex str =~ m/m(.*i)(.*pi)/; did not match all the way till mississip. In fact my initial understanding was that the regex would match mississippi leaving nothing for second regex. Can you throw some light on this .. On 5/31/07, Chas Owens <[EMAIL PROTECTED]> wrote: > On 5/31/07, Sharan Basappa <[EMAIL PROTECTED]> wrote: > snip > > $str =~ m/m(.*i?)(.*pi)/; > snip > > ? only means non-greedy when following a * or a +. When it follows a > pattern it means optional. Nope, because then the match will fail. Greediness will not cause the match to fail; it only affects how much of the string is matched. #!/usr/bin/perl use strict; use warnings; $_ = "abababab"; print "trying /(a.*b)/g\n"; my $i = 1; while (/(a.*b)/g) { print "\t$i: $1\n"; $i++; } print "trying /(a.*?b)/g\n"; $i = 1; while (/(a.*?b)/g) { print "\t$i: $1\n"; $i++; } print "trying /(a.+b)/g\n"; my $i = 1; while (/(a.+b)/g) { print "\t$i: $1\n"; $i++; } print "trying /(a.+?b)/g\n"; $i = 1; while (/(a.+?b)/g) { print "\t$i: $1\n"; $i++; } prints trying /(a.*b)/g 1: abababab trying /(a.*?b)/g 1: ab 2: ab 3: ab 4: ab trying /(a.+b)/g 1: abababab trying /(a.+?b)/g 1: abab 2: abab -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Passing arguments to subroutine
On 31 May 2007 01:27:26 -0700, Alma <[EMAIL PROTECTED]> wrote: -- snip -- unlink($file_path); -- snip -- I'm aware that the question was already answered, but a generic tip for the future. You could try adding a statement like: print "Deleting $file_path\n"; to help debug and ensure the variable got set up properly. You can use it to help track down the source of the problem. Alternatively, you (and I) should learn how to use a Perl debugger ;) @Chas: Good question... I'm fairly new to Perl and everything I know is learnt off the web. I've done stuff like `&myFunction` for zero param functions and going from that to `&myFunction()` isn't such a leap. I suspect one of the tutorials that Google or Perl.org points to has something in it that needs correcting. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: did I get greedy quantifiers wrong ?
Untested code. $str = mississippi; $str =~ m/m(.*i)/; print $1; # Should output mississippi $str = mississippi; $str =~ m/m(.*i)(.*pi)/; This requires $2 to have .*pi in it. Since this is a greedy RegEx, $1 will grab mississi (must end by an i) and leave as little as possible for $2 - ie ppi. Um. Hope this post helped! On 5/31/07, Sharan Basappa <[EMAIL PROTECTED]> wrote: Thanks a lot Paul .. For this rule : $str = mississippi; $str =~ m/m(.*i)(.*pi)/; My initial understanding was that .*i would match all the way till last char i. This would indeed be true if .*i was not followed by .*pi. Do you agree ? -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: did I get greedy quantifiers wrong ?
Thanks a lot Paul .. For this rule : $str = mississippi; $str =~ m/m(.*i)(.*pi)/; My initial understanding was that .*i would match all the way till last char i. This would indeed be true if .*i was not followed by .*pi. Do you agree ? On 31 May 2007 06:11:45 -0700, Paul Lalli <[EMAIL PROTECTED]> wrote: On May 31, 6:02 am, [EMAIL PROTECTED] (Sharan Basappa) wrote: > I seem to be having some conceptual problem with greedy quantifiers .. > My understanding is that it matches as much as follows while still > allowing rest of the > regex to match. 90% correct. The other 10% is that the match starts left-to-right. It will start with the first part of the string that can match, and match as much of *that* as possible. It will not search the rest of the string to see if a longer match is possible later. For example: $string = 'abbaba'; $string =~ /(b*)/; In this case, $1 will be set to 'bb', because that is the *first* longest string it could find, even though if it had continued, it would have been able to find 'b' later. > But look at the following example : > $str = mississippi; > $str =~ m/m(.*i)(.*pi)/; > print "one is $1 \n"; > print "two is $2 \n"; > > $str = mississippi; > $str =~ m/m(.*i?)(.*pi)/; This doesn't mean what you think it means. This tells Perl that the second token - (.*i?) can match as much of anything as it can, followed by 0 or 1 i's. That ? does not apply to the .* unless you put it right after the *. Compare and contrast with: (.*?i), which means to match as little of anything as possible, followed by exactly one i. > print "one is $1 \n"; > print "two is $2 \n"; > > In the first code snippet, I expected first regex (.*i) to match till > ississip Right there is a problem. Your token is (.*i). That is, the last character of this token must be an i. It can't end with a p. That doesn't match. The .* matches as much as it can until the last 'i', then saves the 'i' for the i in the token. > and leave pi for (.*pi) regex. > > But what I get as the output of this script is : > > one is ississi > two is ppi > one is ississip > two is pi > > Why is that perl is leaving ppi to second regex while it can continue > till first p It can't. The token ends in an i. 'i' must be the last thing that (.*i) matches. Paul Lalli -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: did I get greedy quantifiers wrong ?
On May 31, 10:15 am, [EMAIL PROTECTED] (Sharan Basappa) wrote: > Thanks a lot Paul .. > > For this rule : > $str = mississippi; > $str =~ m/m(.*i)(.*pi)/; > > My initial understanding was that .*i would match all the way till last char > i. > This would indeed be true if .*i was not followed by .*pi. > Do you agree ? Yes. Like you said initially - a regexp quantifier will match as much as possible WITHOUT PREVENING THE MATCH FROM SUCEEDING. If the .*i matched all the way up until the very last i, then the .*pi would fail because there'd be nothing left to match. FWIW, you can add: use re 'debug'; to the top of your script to see exactly what Perl does when trying to match a regexp. You'll see in this case that it first tries to let .*i match all the way to the end, but then finds that .*pi now fails, so it backtracks, letting the .* part of .*i match less and less until .*pi can also match. Paul Lalli -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Passing arguments to subroutine
On 5/31/07, yitzle <[EMAIL PROTECTED]> wrote: snip I suspect one of the tutorials that Google or Perl.org points to has something in it that needs correcting. snip Probably more than one thing. I would suggest reading the following books to learn Perl and to only use random tutorials on the Internet as starting points to learn more by reading perldoc. 1. The Llama -- Learning Perl (read cover to cover) 2. The Staghound? -- Perl Best Practices (read cover to cover) 3. The Camel -- Programming Perl (read cover to cover eventually, but focus on areas where you have questions) 4. The Panther -- Advanced Perl Programming (read cover to cover) All told that is about $169.80 plus tax. Of course, since they are all O'Reilly books, you can just sign up for Safari Books for about $20 a month and read them online. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Sending mail without a callback
Have been googleing for a while and don't seem to be able to find a perl library which allows me to send an email without having to resort to a callback. Can someone please point me in the correct direction. Ben -- Ben Edwards - Bristol, UK If you have a problem emailing me use http://www.gurtlush.org.uk/profiles.php?uid=4 (email address this email is sent from may be defunct) -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: did I get greedy quantifiers wrong ?
Thanks Paul, Yitzle .. On 31 May 2007 07:28:26 -0700, Paul Lalli <[EMAIL PROTECTED]> wrote: On May 31, 10:15 am, [EMAIL PROTECTED] (Sharan Basappa) wrote: > Thanks a lot Paul .. > > For this rule : > $str = mississippi; > $str =~ m/m(.*i)(.*pi)/; > > My initial understanding was that .*i would match all the way till last char i. > This would indeed be true if .*i was not followed by .*pi. > Do you agree ? Yes. Like you said initially - a regexp quantifier will match as much as possible WITHOUT PREVENING THE MATCH FROM SUCEEDING. If the .*i matched all the way up until the very last i, then the .*pi would fail because there'd be nothing left to match. FWIW, you can add: use re 'debug'; to the top of your script to see exactly what Perl does when trying to match a regexp. You'll see in this case that it first tries to let .*i match all the way to the end, but then finds that .*pi now fails, so it backtracks, letting the .* part of .*i match less and less until .*pi can also match. Paul Lalli -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Sending mail without a callback
On 5/31/07, Ben Edwards <[EMAIL PROTECTED]> wrote: Have been googleing for a while and don't seem to be able to find a perl library which allows me to send an email without having to resort to a callback. Can someone please point me in the correct direction. snip use Mail::Sender; my $sender = Mail::Sender->new( { smtp => 'mail.yourdomain.com', from => '[EMAIL PROTECTED]' } ); $sender->MailFile( { to => '[EMAIL PROTECTED]', subject => 'Here is the file', msg => "I'm sending you the list you wanted.", file=> 'filename.txt' } ); -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Outlook CSV Parser
"Mumia W." schreef: > Laxminarayan G Kamath A: >> http://download.deeproot.in/~kamathln/outlook-encrtypted-sample.csv > > Well I asked for it. :-) > > It's impossible to tell where one record ends and another record > begins with that file. Maybe not, because the rule was that it ends at newline, unless inside quotes. -- Affijn, Ruud "Gewoon is een tijger." -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
ScanAlert XSS warnings
Hello, there is a script on our site, that receives this warning from the HackSafe scanalerts" -- " ... The remote web application appears to be vulnerable to cross site scripting (XSS). General Solution: HTML encode data before sending it to the browser. Filtering < and > alone will not solve all cross site scripting attacks. It is suggested you also attempt to filter out open and closing parenthesis or convert them to their encoded equivalents. ... " I have gone through the script serveral times and though we had it corrected. Has anyone on the list experience this problem and may have some suggestions on how to correct this XSS scripting. This takes a POST from a standard type registration form. TIA, Mike(mickalo)Blezien === Thunder Rain Internet Publishing === -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Passing arguments to subroutine
On May 31, 10:15 am, [EMAIL PROTECTED] (Yitzle) wrote: > I suspect one of the tutorials that Google or Perl.org points to has > something in it that needs correcting. Actually, it's an unfortunate truth that up until Edition 3, the Llama itself recommended that you use the & to call subroutines... Paul Lalli -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: ScanAlert XSS warnings
On 5/31/07, Mike Blezien <[EMAIL PROTECTED]> wrote: there is a script on our site, that receives this warning from the HackSafe scanalerts" -- " ... The remote web application appears to be vulnerable to cross site scripting (XSS). Have you seen these reference pages? http://www.cgisecurity.com/articles/xss-faq.shtml http://en.wikipedia.org/wiki/Cross-site_scripting Hope this helps! --Tom Phoenix Stonehenge Perl Training -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: ScanAlert XSS warnings
Tom, - Original Message - From: "Tom Phoenix" <[EMAIL PROTECTED]> To: "Mike Blezien" <[EMAIL PROTECTED]> Cc: "Perl List" Sent: Thursday, May 31, 2007 1:46 PM Subject: Re: ScanAlert XSS warnings On 5/31/07, Mike Blezien <[EMAIL PROTECTED]> wrote: there is a script on our site, that receives this warning from the HackSafe scanalerts" -- " ... The remote web application appears to be vulnerable to cross site scripting (XSS). Have you seen these reference pages? http://www.cgisecurity.com/articles/xss-faq.shtml http://en.wikipedia.org/wiki/Cross-site_scripting Hope this helps! --Tom Phoenix Stonehenge Perl Training Thanks for the information. Mike -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: IO::Socket::INET client hangs on no server on windoze
On May 29, 7:55 pm, [EMAIL PROTECTED] (kenTk) wrote: > On May 29, 2:40 pm, [EMAIL PROTECTED] (Zentara) wrote: > > > On 28 May 2007 08:28:35 -0700, [EMAIL PROTECTED] (kenTk) wrote: > > > >If there is no server or no connection this hangs for about 20 seconds > > >and then crashes with the following error message. > > >The Timeout seems to make no difference and the 'warn' does not occur. > > >If works file with a good connection to a server. > > >Using 5.8.8 active perl. > > >Anyone got any suggestions for making it return undef in an orderly > > >fashion? > > >(I have tested it on Linux and it seems to work fine both ways) > > > >use strict; > > >use warnings; > > >use IO::Socket::INET; > > > >sub testSocket > > >{ > > > my $sock; > > > my $ok=0; > > > unless( $sock=IO::Socket::INET->new(192.168.5.41, > > > PeerPort => '21', > > > Proto=> 'tcp', > > > Timeout =>'3')) > > > are you sure that '21' and '3' should be quoted? > > Maybe Perl figures it's context and does the right thing on linux? > > > > { warn 'Cant connect'; } > > > else > > > { $sock->close; $ok=1; } > > > return $ok; > > >} > > > I don't use win32, but have seen these somewhat related > > posts:http://perlmonks.org?node_id=567912 > > >http://perlmonks.org?node_id=529812 > > > zentara > > I tried removing the quotes around 21 and 3 but with no success. > I have already tried the ioctl fix for non-blocking on windoze but to > no avail. I believe that only applies to a new socket object after it > has been created. In this case that creation process fails with a > crash and doesnt return in an orderly fashion. I wish that I didn't > have to use windoze but .. I have now found that this behaves correctly a couple of other windoze win32 machines. There must be something bust deep down on this machine. Strange because I have never seen it on any other application on this machine. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Passing arguments to subroutine
On May 31, 9:27 am, [EMAIL PROTECTED] (Alma) wrote: > sub delete_file() > { > my $self = shift; > my $file_path = @_; That sets $file_path to the number of arguments in the call to the method delete_file(). To set $file_path to the first of the arguments in the call to the method delete_file()... my ($file_path) = @_; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Error:Can't locate object method "prepare" via package "abc" at xyz.pm
Hi , Urgent help. I have written 2 modules one abc.pm & xyz.pm (admin modules). abc.pm -- my $databasehandle; sub new($){ my ($self,$usr,$pwd) = @_; $usr||= "test"; $pwd ||= "test123"; ($self) = {}; bless($self); $databasehandle = DBI->connect("DBI:Pg:dbname=mydb",$usr,$pwd, {PrintError =>1}); if (!$databasehandle){ print "Database connection is not estabilished"; exit; } return($self); } sub DESTROY(){ my $self; $self->disconnect(); } sub disconnect(){ $databasehandle->disconnect() if ($databasehandle); } xyz.pm -- package admin; use DBI; use Apache::DBI; use CGI; use abc; use strict; my $database_handle =(); sub new() { my $self={}; bless($self); $database_handle = abc->new('test','test123'); if (!$database_handle){ print "connection cant done:"; return -1; } print " connection done:"; return $self; } sub display() { my $self = shift; my $sth = $database_handle->prepare("select * from table where status ='P'"); print $sth; my $res =$sth->execute(); my @row = $sth->fetchrow_array(); print @row; return $res; } I am getting an error : Can't locate object method "prepare" via package "abc" at xyz.pm Is it not possible the way i am trying...can anyone tell me where i am wrong. Thanks , Alma -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Arrow dereference operator question
On May 31, 7:55 am, [EMAIL PROTECTED] (David Unric) wrote: > Based on perlref documentation arrow operator between brackets subscripts > may be omitted so the following code is valid: > > @array = ( [1, 2], [3, 4] ); > $element = $array[0][0];# shorthand for $element = $array[0]->[0] > > Could somebody explain why it causes syntax error when the above rule is > applied to returned value of a subroutine ? > > sub mysub { > @array = ( [1, 2], [3, 4] ); > > return @array; > > } > > $element = (&mysub)[0][0]; # $elem = (&mysub)[0]->[0] is valid > -- > syntax error at testarr.pl line 7, near "][" the sub mysub should pass an array ref ([EMAIL PROTECTED]) not the array (@array). -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: did I get greedy quantifiers wrong ?
On May 31, 3:02 pm, [EMAIL PROTECTED] (Sharan Basappa) wrote: > I seem to be having some conceptual problem with greedy quantifiers .. > My understanding is that it matches as much as follows while still > allowing rest of the > regex to match. > But look at the following example : > $str = mississippi; > $str =~ m/m(.*i)(.*pi)/; > print "one is $1 \n"; > print "two is $2 \n"; > > $str = mississippi; > $str =~ m/m(.*i?)(.*pi)/; > print "one is $1 \n"; > print "two is $2 \n"; > > In the first code snippet, I expected first regex (.*i) to match till > ississip and leave pi for (.*pi) regex. > > But what I get as the output of this script is : > > one is ississi > two is ppi > one is ississip > two is pi > > Why is that perl is leaving ppi to second regex while it can continue > till first p in ppi and can still get the second regex to get a match > ? > $str =~ m/m(.*i)(.*pi)/; m - matches m (.*i) - matched ississi. ( IF u want to match p of ppi u will have to write (.*i.)) -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Passing arguments to subroutine
On May 31, 9:27 am, [EMAIL PROTECTED] (Alma) wrote: > Hi All, > > I need to pass the result of prepare statement as an argument to the > subroutine. > > sub abc() > { > my $self= shift; > my($id,$title) = @_; > my $sth1= $databasehandle->prepare("select file_path from xyz > where id='$id' and title like '$title'"); > my $res = $sth1->execute(); > > my @row = $sth1->fetchrow_array; > print @row; > > &deleteposter_file(@row); > > } > > #-Deletes from the File system > > sub delete_file() > { > my $self = shift; > my $file_path = @_; > > # extract the file name > # my @parts = split('\/',$file_path); > # my $file =$parts[$#parts]; > # #unlink($file); > # if( -e "file" ) { > # system("rm $file_path"); > > unlink($file_path); > > } Your split statement seems to be wrong. split uses a regex to match so it should be: split /\\/, $file_path; I'd also suggest making sure you're in the correct directory to use unlink. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: did I get greedy quantifiers wrong ?
On May 31, 11:02 am, [EMAIL PROTECTED] (Sharan Basappa) wrote: > I seem to be having some conceptual problem with greedy quantifiers .. > My understanding is that it matches as much as follows while still > allowing rest of the regex to match. Yes. That is correct. > $str = mississippi; > $str =~ m/m(.*i)(.*pi)/; > I expected first regex (.*i) to match till > ississip No the subpattern (.*i) can't match 'ississip' because the last character of 'ississip' is not 'i'. You seem to have forgotten the ' while still allowing rest of the regex to match' bit. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Arrow dereference operator question
On May 31, 7:55 am, [EMAIL PROTECTED] (David Unric) wrote: > Based on perlref documentation arrow operator between brackets subscripts > may be omitted so the following code is valid: > > @array = ( [1, 2], [3, 4] ); > $element = $array[0][0];# shorthand for $element = $array[0]->[0] > > Could somebody explain why it causes syntax error when the above rule is > applied to returned value of a subroutine ? > > sub mysub { > @array = ( [1, 2], [3, 4] ); > > return @array; > > } > > $element = (&mysub)[0][0]; # $elem = (&mysub)[0]->[0] is valid > -- > syntax error at testarr.pl line 7, near "][" the sub mysub should pass an array ref ([EMAIL PROTECTED]) not the array (@array). -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: encode UTF8 -> MIME
On May 30, 5:58 pm, [EMAIL PROTECTED] (Chas Owens) wrote: > On 30 May 2007 06:07:55 -0700, cc96ai <[EMAIL PROTECTED]> wrote: > snip> I have a UTF8 input > > $value = "%23%C2%A9%C2%AE%C3%98%C2%A5%C2%BC%C3%A9%C3%8B > > %C3%B1%C3%A0%C3%A6%3F%23"; > > > the HTML output should be > > ">#(c)(r)Ø¥¼éËñàæ?#"; > > > but I cannot find a way to convert it > > snip > > #!/usr/bin/perl > use strict; > use warnings; > use URI::Escape; > > my $s = '%C3%A9'; > > print uri_unescape($s), "\n"; > > This prints > é > for me. But Perl doesn't actually print an e-acute character! It prints the _byte_ sequence "\xC3\xA9". Now if you happen to print this byte sequence to a device that's expecting UTF8 the it'll be rendered as an e-acute. Remember, in Perl there are two types of string, Unicode strings (unfortunately known as "utf8 strings") and byte strings. I suspect the OP wants to decode '%C3%A9' into a single character string containing e-acute, not the two-byte byte string "\xC3\xA9". Oddly, there's a uri_unescape_utf8 but no uri_unescape_utf8 provided by URI::Escape. However combining URI::Escape::uri_unescape() and Encode::decode_utf8() in one statement is not overly taxing. use Encode; use URI::Escape qw(uri_unescape); my $e_accute = decode_utf8 uri_unescape '%C3%A9'; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Error:Can't locate object method "prepare" via package "abc" at xyz.pm
Alma wrote: Hi , Urgent help. I have written 2 modules one abc.pm & xyz.pm (admin modules). abc.pm -- my $databasehandle; Note that this establishes a single $databasehandle for every object of type 'abc' that you create; it does not create a separate one for each object. sub new($){ my ($self,$usr,$pwd) = @_; Again, you have a signature problem. 'sub new($)' says that 'new' will take a single scalar as a parameter; as such, @_ will only ever have one value in it: $usr and $pwd will always be set to null. Also, read up on the syntax of 'bless' a bit more. IIRC, saying 'bless $self;' is not enough. $usr||= "test"; $pwd ||= "test123"; ...and thus $usr and $pwd will always equal "test" and "test123", respectively. sub DESTROY(){ my $self; $self->disconnect(); } You never set $self to anything. Change the '()' in 'sub DESTROY()' to '($)', or remove them altogether; then change 'my $self;' to 'my $self = shift;' or 'my ($self) = @_;' -- Jonathan "Dataweaver" Lang -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: encode UTF8 -> MIME
> Oddly, there's a uri_unescape_utf8 but no > uri_unescape_utf8 provided > by URI::Escape. > > However combining URI::Escape::uri_unescape() and > Encode::decode_utf8() > in one statement is not overly taxing. > > use Encode; > use URI::Escape qw(uri_unescape); > my $e_accute = decode_utf8 uri_unescape '%C3%A9'; > Is %C3 equal to à Capital A, tilde and %A9 equal to © Copyright. ? So you are trying to convert to HTML Flash found here http://www.allwebco-templates.com/support/S_hex.htm? http://dsmith1.userworld.com/html_css/derek.htm Yahoo! oneSearch: Finally, mobile search that gives answers, not web links. http://mobile.yahoo.com/mobileweb/onesearch?refer=1ONXIC -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Error:Can't locate object method "prepare" via package "abc" at xyz.pm
On 5/31/07, Jonathan Lang <[EMAIL PROTECTED]> wrote: snip Again, you have a signature problem. 'sub new($)' says that 'new' will take a single scalar as a parameter; as such, @_ will only ever have one value in it: $usr and $pwd will always be set to null. snip Well, there is a prototype problem, but it isn't that $ will force new to only accept one value, but rather that prototypes and OO Perl don't mix. Perl simply ignores prototypes on methods. Also prototypes are broken*, don't use them. #!/usr/bin/perl use strict; use warnings; package foo; sub new ($) { my $class = shift; return bless { @_ }, $class; } package main; my $foo = foo->new(this => 1, that => 2); print "this $foo->{this} and that $foo->{that}\n"; * http://library.n0i.net/programming/perl/articles/fm_prototypes/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Error:Can't locate object method "prepare" via package "abc" at xyz.pm
On 31 May 2007 06:17:50 -0700, Alma <[EMAIL PROTECTED]> wrote: snip I am getting an error : Can't locate object method "prepare" via package "abc" at xyz.pm snip Besides the things that are wrong that other people have mentioned, you need to make the abc class a subclass of DBI. This being Perl there are several ways to do it, but I prefer to say package DBI::MySingleton; use strict; use Carp; use base 'DBI'; our $dbh; sub new { our $dbh; my ($class, $user, $pass) = @_; $user ||= "test"; $pass ||= "test123"; unless ($dbh and $dbh->ping) { $dbh = DBI->connect("DBI:Pg:dbname=mydb",$user,$pass, {PrintError =>1}) or croak DBI->errstr; bless $dbh, $class; } return $dbh; } sub DESTROY() { my $self = shift; $self->disconnect(); } 1; Or you could just use DBI->connect_cached() and save yourself a heap of trouble. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Error:Can't locate object method "prepare" via package "abc" at xyz.pm
On 05/31/2007 08:17 AM, Alma wrote: [...] $database_handle = abc->new('test','test123'); [...] No, the 'new' method of 'abc' returns an object of type 'abc'--not a database handle. Review the return statement in abc::new again. sub display() { my $self = shift; my $sth = $database_handle->prepare("select * from table where status This fails because $database_handle is not a database handle. You can force it to work by defining a 'prepare' method in the 'abc' package. [...] I am getting an error : Can't locate object method "prepare" via package "abc" at xyz.pm Is it not possible the way i am trying...can anyone tell me where i am wrong. Thanks , Alma -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Error:Can't locate object method "prepare" via package "abc" at xyz.pm
Chas Owens wrote: Well, there is a prototype problem, but it isn't that $ will force new to only accept one value, but rather that prototypes and OO Perl don't mix. Perl simply ignores prototypes on methods. Also prototypes are broken*, don't use them. -snip- * http://library.n0i.net/programming/perl/articles/fm_prototypes/ "Broken" and "don't use them" is a bit extreme. But I will agree with the general sentiment that they should not be used as a matter of course; they should be reserved for a handful of special cases where they help more than they harm. -- Jonathan "Dataweaver" Lang -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Error:Can't locate object method "prepare" via package "abc" at xyz.pm
On 5/31/07, Jonathan Lang <[EMAIL PROTECTED]> wrote: snip Also prototypes are broken*, don't use them. -snip- > * http://library.n0i.net/programming/perl/articles/fm_prototypes/ "Broken" and "don't use them" is a bit extreme. But I will agree with the general sentiment that they should not be used as a matter of course; they should be reserved for a handful of special cases where they help more than they harm. snip Yes, it is extreme, and, yes, there are a few occasions where they are useful; however, you will never need them and dealing with -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Effective date grab
A while ago I had posted requesting help with a long block of code that would do all kinds of stuff dealing with the date. It turned out to not work despite being technically, correct. Instead of getting help with it, Mr. Phoenix provided me with a block of code that did what I needed but much more concisely. And, more importantly, correctly. for (1 .. 7) { $time -= 24*60*60; my @date = (localtime($time))[3 .. 5]; push @days, (sprintf '%02d', $date[0]); push @months,(sprintf '%02d',$date[1] + 1); push @years, $date[2] + 1900; push @searchDate, join "-", ($date[2] + 1900), (sprintf '%02d',$date[1] + 1), (sprintf '%02d', $date[0]); } This will give me a weeks worth of dates regardless of whether or not the month flips over in the middle of the week. What I'd like to do now is modify this or figure out a similar block of code that will do the same only for an entire month. The thing I see being a problem though is that some months have 30 days, some months have 31 days and February has 28/29 days. This makes it pretty much impossible to just do a for loop utilizing (1..whatever). How can I populate an array of an entire month's worth of dates without worrying about how many days the month has? Mathew -- Keep up with me and what I'm up to: http://theillien.blogspot.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Effective date grab
Too tired to write real code. Pseudo-code: use timelocal; @curTime = localtime; $curTime[3] = 1; # Set to first of the month $time = timelocal @curTime; # The 'time' as of the forst of the month $thisMonth = $curTime[4]; $dayLength = 24*60*60; do { @dataToUse = localtime $time; # do the pushing and formatting here $time += $dayLength; # Next day $curMonth = (localtime $time)[4]; } while ($thisMonth eq $curMonth); Basically, grab the current month. Iterate from the first until the last by adding 24*60*60 to the $time until (localtime $time)[4] - the month based on $time - changes. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Effective date grab
Yes, a month from the first to the last day. This is how I'm doing it right now which seems to be a lot of code which should be able to be pruned: #Declare the variables we'll need my $count = 1; my $febDays; my @days; my %time; my %months31 = ( "01" => undef, "03" => undef, "05" => undef, "07" => undef, "08" => undef, "10" => undef, "12" => undef, ); my %months30 = ( "04" => undef, "06" => undef, "09" => undef, "11" => undef, ); my %months = ( "01" => "Jan", "02" => "Feb", "03" => "Mar", "04" => "Apr", "05" => "May", "06" => "Jun", "07" => "Jul", "08" => "Aug", "09" => "Sept", "10" => "Oct", "11" => "Nov", "12" => "Dec", ); my @date; # = (localtime)[4,5]; my $month = (sprintf '%02d', (localtime)[4]); #$date[0]); my $year = (localtime)[5] + 1900; #$date[1] + 1900; # If the value of the 'month' variable is '00' we are in January so need # to set the $month to 12 in order to get December's data. Any other # value is spot on. The reason being that while (localtime) will produce # a value based on a startpoint of '00', we are shifting this back one by # making '00' equal to '12' causing all other numbers to match up to their # respective month. if ($month == '00') { $month = 12; $year--; } # We need to determine if the current year is a leap year so we use the # right number of days for Feb. if (isleap($year)) { $febDays = 29; }else{ $febDays = 28; } Determine if the month requested has 31, 30 or 28 days and build # our days array to match if (exists($months31{$month})){ while ($count <= 31) { push @days, (sprintf '%02d', $count); $count++; } }elsif (exists($months30{$month})){ while ($count <= 30) { push @days, (sprintf '%02d', $count); $count++; } }else{ while ($count <= $febDays) { push @days, (sprintf '%02d', $count); $count++; } } That's just a keister-load of code just to create an array of 28/29, 30 or 31 dates. Mathew Keep up with me and what I'm up to: http://theillien.blogspot.com Mathew Snyder wrote: > A while ago I had posted requesting help with a long block of code that would > do > all kinds of stuff dealing with the date. It turned out to not work despite > being technically, correct. Instead of getting help with it, Mr. Phoenix > provided me with a block of code that did what I needed but much more > concisely. > And, more importantly, correctly. > > for (1 .. 7) { > $time -= 24*60*60; > my @date = (localtime($time))[3 .. 5]; > push @days, (sprintf '%02d', $date[0]); > push @months,(sprintf '%02d',$date[1] + 1); > push @years, $date[2] + 1900; > push @searchDate, join "-", ($date[2] + 1900), (sprintf '%02d',$date[1] + > 1), > (sprintf '%02d', $date[0]); > } > > This will give me a weeks worth of dates regardless of whether or not the > month > flips over in the middle of the week. > > What I'd like to do now is modify this or figure out a similar block of code > that will do the same only for an entire month. The thing I see being a > problem > though is that some months have 30 days, some months have 31 days and February > has 28/29 days. This makes it pretty much impossible to just do a for loop > utilizing (1..whatever). How can I populate an array of an entire month's > worth > of dates without worrying about how many days the month has? > > Mathew -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Effective date grab
> Yes, a month from the first to the last day. This is how I'm doing it > right > now which seems to be a lot of code which should be able to be pruned: > #Declare the variables we'll need > my $count = 1; > my $febDays; > my @days; > my %time; > my %months31 = ( > "01" => undef, > "03" => undef, > "05" => undef, > "07" => undef, > "08" => undef, > "10" => undef, > "12" => undef, > ); > my %months30 = ( > "04" => undef, > "06" => undef, > "09" => undef, > "11" => undef, > ); > my %months = ( > "01" => "Jan", > "02" => "Feb", > "03" => "Mar", > "04" => "Apr", > "05" => "May", > "06" => "Jun", > "07" => "Jul", > "08" => "Aug", > "09" => "Sept", > "10" => "Oct", > "11" => "Nov", > "12" => "Dec", > ); > > my @date; # = (localtime)[4,5]; > my $month = (sprintf '%02d', (localtime)[4]); #$date[0]); > my $year = (localtime)[5] + 1900; #$date[1] + 1900; > > # If the value of the 'month' variable is '00' we are in January so need > # to set the $month to 12 in order to get December's data. Any other > # value is spot on. The reason being that while (localtime) will produce > # a value based on a startpoint of '00', we are shifting this back one by > # making '00' equal to '12' causing all other numbers to match up to their > # respective month. > if ($month == '00') { > $month = 12; > $year--; > } > > # We need to determine if the current year is a leap year so we use the > # right number of days for Feb. > if (isleap($year)) { > $febDays = 29; > }else{ > $febDays = 28; > } > > Determine if the month requested has 31, 30 or 28 days and build > # our days array to match > if (exists($months31{$month})){ > while ($count <= 31) { > push @days, (sprintf '%02d', $count); > $count++; > } > }elsif (exists($months30{$month})){ > while ($count <= 30) { > push @days, (sprintf '%02d', $count); > $count++; > } > }else{ > while ($count <= $febDays) { > push @days, (sprintf '%02d', $count); > $count++; > } > } > > That's just a keister-load of code just to create an array of 28/29, 30 or > 31 dates. Yes. You might want to think about this rather common snippet of code that pops up from time to time Assuming you input a month ( $mon ) like February as a 2, this may help my @days_month = (31,28,31,30,31,30,31,31,30,31,30,31); # Number of days in a month if ($mon == 2 && ((!($year%4)) && (($year%100) || (!($year%400){ $days_month = 29; } else{ $days_month = $days_month[$mon-1]; } = -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/