Re: subroutine returning data
Hi all, On Tue, 05 Jun 2012 10:15:36 -0700 John W. Krahn jwkr...@shaw.ca wrote: [ Please do not top-post your replies. Please remove non-relevant text from your reply before posting. TIA ] don't insult and dismiss out of hand the findings of those who take the time to help you. If I insulted Shlomi then I apologize. But he was also just trying to help Chris Stinemetz. Just for the record, I do not recall being particularly insulted from John's reply to my reply to his post. I mentioned some problems that I perceived in his code, and he asked me why I think so. It wasn't uncivil or insulting. So no need to apologise or to accuse John of being insulting. Regards, Shlomi Fish -- - Shlomi Fish http://www.shlomifish.org/ http://www.shlomifish.org/humour/ways_to_do_it.html Had I not been already insane, I would have long ago driven myself mad. — The Enemy and how I Helped to Fight It Please reply to list if it's a mailing list post - http://shlom.in/reply . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: subroutine returning data
Hello John, On Mon, 04 Jun 2012 14:19:27 -0700 John W. Krahn jwkr...@shaw.ca wrote: Chris Stinemetz wrote: I have a subroutine that I want to return 1 only if the value of %{$href-{$_[0]}} is equal to 'ND' for the whole 24 occurences. One way to do it: sub site_offAir { return values %{ $href-{ $_[ 0 ] } } == grep( $_ eq 'ND', values %{ $href-{ $_[ 0 ] } } ) ? 1 : ''; } I see several problems with your code: 1. It's quite hard to understand the logic of it. 2. It won't stop when it encounter the first ND value. 3. You have the values % { $href-{ $_[0] } } gob twice (a duplicate expression). 4. You've used $_[0] which is a positional parameter, see: http://perl-begin.org/tutorials/bad-elements/#subroutine-arguments 5. The grep does not uses braces for its predicate/block which is harder to read. 6. You will return a true value (a list of length 1) when the function is called in list context. I prefer the suggestions in the previous sub-thread with the looping and returning. Other ones can be written in a good manner using List::MoreUtils' any/all/none/notall , and the smart match operator. While we are discussing bad solutions, another option is to do: # Bad code - don't use sub site_offAir { my ($hash_ref) = @_; my @v = keys(reverse(%$hash_ref)); return ((@v == 1) and ($v[0] eq 'ND')); } But please don't use it. Regards, Shlomi Fish -- - Shlomi Fish http://www.shlomifish.org/ Best Introductory Programming Language - http://shlom.in/intro-lang danderson “We are NO LONGER the knights who say ‘BitKeeper’. We are now the knights who say ‘git, git, git, cogito — Linus!’.” Please reply to list if it's a mailing list post - http://shlom.in/reply . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: subroutine returning data
Shlomi Fish wrote: On Mon, 04 Jun 2012 14:19:27 -0700 John W. Krahnjwkr...@shaw.ca wrote: Chris Stinemetz wrote: I have a subroutine that I want to return 1 only if the value of %{$href-{$_[0]}} is equal to 'ND' for the whole 24 occurences. One way to do it: sub site_offAir { return values %{ $href-{ $_[ 0 ] } } == grep( $_ eq 'ND', values %{ $href-{ $_[ 0 ] } } ) ? 1 : ''; } I see several problems with your code: You are entitled to your opinion. 1. It's quite hard to understand the logic of it. No it isn't. (IMHO) 2. It won't stop when it encounter the first ND value. That is true. And your point? 3. You have the values % { $href-{ $_[0] } } gob twice (a duplicate expression). Yes. And... 4. You've used $_[0] which is a positional parameter, So? see: http://perl-begin.org/tutorials/bad-elements/#subroutine-arguments Your argument on your web page does not appear to apply to this situation. 5. The grep does not uses braces for its predicate/block which is harder to read. In your opinion. I prefer to not use braces unless I have to. 6. You will return a true value (a list of length 1) when the function is called in list context. Yes, just as the OP's code. John -- Any intelligent fool can make things bigger and more complex... It takes a touch of genius - and a lot of courage to move in the opposite direction. -- Albert Einstein -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
RE: subroutine returning data
ProTip: If you're going to ask for help, don't insult and dismiss out of hand the findings of those who take the time to help you. -Original Message- From: John W. Krahn [mailto:jwkr...@shaw.ca] Sent: Tuesday, June 05, 2012 2:18 AM To: Perl Beginners Subject: Re: subroutine returning data Shlomi Fish wrote: On Mon, 04 Jun 2012 14:19:27 -0700 John W. Krahnjwkr...@shaw.ca wrote: Chris Stinemetz wrote: I have a subroutine that I want to return 1 only if the value of %{$href-{$_[0]}} is equal to 'ND' for the whole 24 occurences. One way to do it: sub site_offAir { return values %{ $href-{ $_[ 0 ] } } == grep( $_ eq 'ND', values %{ $href-{ $_[ 0 ] } } ) ? 1 : ''; } I see several problems with your code: You are entitled to your opinion. 1. It's quite hard to understand the logic of it. No it isn't. (IMHO) 2. It won't stop when it encounter the first ND value. That is true. And your point? 3. You have the values % { $href-{ $_[0] } } gob twice (a duplicate expression). Yes. And... 4. You've used $_[0] which is a positional parameter, So? see: http://perl-begin.org/tutorials/bad-elements/#subroutine-arguments Your argument on your web page does not appear to apply to this situation. 5. The grep does not uses braces for its predicate/block which is harder to read. In your opinion. I prefer to not use braces unless I have to. 6. You will return a true value (a list of length 1) when the function is called in list context. Yes, just as the OP's code. John -- Any intelligent fool can make things bigger and more complex... It takes a touch of genius - and a lot of courage to move in the opposite direction. -- Albert Einstein -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
List behavior pro tips (was: Re: subroutine returning data)
Even more pro tips: * Let the list moderator (ahem, that would be me) deal with policing the behavior of the list participants. * Realize that this list is publicly archived and indexed by search engines. Realize that potential employers often Google applicants. Realize your behavior here, as such, is in public and reflects on you for better or worse. * Seriously, LET THE LIST MODERATOR DEAL WITH POLICING BEHAVIOR. If you feel like you *absolutely* *must* *say* _SOMETHING_ -- say it directly to me. If it just can't wait for email, I'm on IRC on freenode and irc.perl; /msg genehack and I'll respond. Otherwise, if you find yourself responding, on-list, in a confrontational way, STOP. Just don't send that email. Please. * The whole thing with calling out other peoples responses is getting really old. If you think somebody gave a bad or uninformative response, the best way to deal with that is to write a better one, and explain why it's better, ideally without specifically referring to the other person's post. * Remember that we're here to help Perl beginners. We're not here to snipe at each others answers. That doesn't help beginners. Thanks to all the beginners asking questions, and all the people providing answers (and sorry for not being one of those people more often). You are all appreciated. thanks, john. -- John SJ Anderson / geneh...@genehack.org On Tuesday, June 5, 2012 at 11:08 AM, Jack Maney wrote: ProTip: If you're going to ask for help, don't insult and dismiss out of hand the findings of those who take the time to help you. -Original Message- From: John W. Krahn [mailto:jwkr...@shaw.ca] Sent: Tuesday, June 05, 2012 2:18 AM To: Perl Beginners Subject: Re: subroutine returning data Shlomi Fish wrote: On Mon, 04 Jun 2012 14:19:27 -0700 John W. Krahnjwkr...@shaw.ca (mailto:jwkr...@shaw.ca) wrote: Chris Stinemetz wrote: I have a subroutine that I want to return 1 only if the value of %{$href-{$_[0]}} is equal to 'ND' for the whole 24 occurences. One way to do it: sub site_offAir { return values %{ $href-{ $_[ 0 ] } } == grep( $_ eq 'ND', values %{ $href-{ $_[ 0 ] } } ) ? 1 : ''; } I see several problems with your code: You are entitled to your opinion. 1. It's quite hard to understand the logic of it. No it isn't. (IMHO) 2. It won't stop when it encounter the first ND value. That is true. And your point? 3. You have the values % { $href-{ $_[0] } } gob twice (a duplicate expression). Yes. And... 4. You've used $_[0] which is a positional parameter, So? see: http://perl-begin.org/tutorials/bad-elements/#subroutine-arguments Your argument on your web page does not appear to apply to this situation. 5. The grep does not uses braces for its predicate/block which is harder to read. In your opinion. I prefer to not use braces unless I have to. 6. You will return a true value (a list of length 1) when the function is called in list context. Yes, just as the OP's code. John -- Any intelligent fool can make things bigger and more complex... It takes a touch of genius - and a lot of courage to move in the opposite direction. -- Albert Einstein -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org (mailto:beginners-unsubscr...@perl.org) For additional commands, e-mail: beginners-h...@perl.org (mailto: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: subroutine returning data
[ Please do not top-post your replies. Please remove non-relevant text from your reply before posting. TIA ] Jack Maney wrote: ProTip: The top two results from Google state: PROTIP | Know Your Meme About PROTIP is a term often used in forums and comments to preface snarky, obvious, counterintuitive, or sometimes genuine advice for the novice. Its usage. knowyourmeme.com/memes/protip - Cached # Urban Dictionary: protip Obvious advice sarcastically presented as sage wisdom. www.urbandictionary.com/define.php?term=protip - Cached - Similar If you are trying to imply that you are a professional then snarky or sarcastic comments do not bode well. If you're going to ask for help, I was providing code to Chris Stinemetz who *was* asking for help. don't insult and dismiss out of hand the findings of those who take the time to help you. If I insulted Shlomi then I apologize. But he was also just trying to help Chris Stinemetz. John -- Any intelligent fool can make things bigger and more complex... It takes a touch of genius - and a lot of courage to move in the opposite direction. -- Albert Einstein -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: subroutine returning data
On Mon, Jun 04, 2012 at 11:30:30AM -0500, Chris Stinemetz wrote: I have a subroutine that I want to return 1 only if the value of %{$href-{$_[0]}} is equal to 'ND' for the whole 24 occurences. Any suggestions is greatly appreciated. Thank you, Chris sub site_offAir { for (values %{$href-{$_[0]}}) { return 1 if $_ eq 'ND'; #need to test all values are eq to 'ND' } return ''; } I would imagine it to be much easier to look at it from the other way. Return 0 any time you find a value that does not equal NO. Then return 1 at the end. -- Paul Johnson - p...@pjcj.net http://www.pjcj.net -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: subroutine returning data
On Jun 4, 2012, at 11:30 AM, Chris Stinemetz wrote: I have a subroutine that I want to return 1 only if the value of %{$href-{$_[0]}} is equal to 'ND' for the whole 24 occurences. Any suggestions is greatly appreciated. Chris, I don't know how to read your hash directly (hash of hashes?) but here's some logic that might help get the response you want,: my %VAR1 = ( '00' = 'ND', '01' = 'ND', '02' = 'ND', '10' = 'ND', '03' = 'ND', '11' = 'ND', '20' = 'ND', '04' = 'ND', '12' = 'ND', '21' = 'ND', '05' = 'ND', '13' = 'ND', '22' = 'ND', '06' = 'ND', '14' = 'ND', '23' = 'ND', '07' = 'ND', '15' = 'ND', '08' = 'ND', '16' = 'ND', '09' = 'ND', '17' = 'ND', '18' = 'ND', '19' = 'ND', '19' = 'oD' ); print my $test = (test_hash); sub test_hash { my $return = 1; while ( my($k,$v) = each %VAR1 ) { if ($v ne 'ND') { $return = 0; } } return ($return); } -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: subroutine returning data
On 12-06-04 12:30 PM, Chris Stinemetz wrote: I have a subroutine that I want to return 1 only if the value of %{$href-{$_[0]}} is equal to 'ND' for the whole 24 occurences. Any suggestions is greatly appreciated. Thank you, Chris sub site_offAir { for (values %{$href-{$_[0]}}) { return 1 if $_ eq 'ND'; #need to test all values are eq to 'ND' return if $_ ne 'ND'; } return ''; return 1; } -- Just my 0.0002 million dollars worth, Shawn Programming is as much about organization and communication as it is about coding. _Perl links_ official site : http://www.perl.org/ beginners' help : http://learn.perl.org/faq/beginners.html advance help: http://perlmonks.org/ documentation : http://perldoc.perl.org/ news: http://perlsphere.net/ repository : http://www.cpan.org/ blog: http://blogs.perl.org/ regional groups : http://www.pm.org/ -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: subroutine returning data
Thanks Shawn! The values %{$href-{$_[0]}} code is pretty ugly but I get it now. And it make sense to break out of the loop as soon as you don't pass the test. Kindest Regards, Bill Stephenson On Jun 4, 2012, at 12:49 PM, Shawn H Corey wrote: On 12-06-04 12:30 PM, Chris Stinemetz wrote: I have a subroutine that I want to return 1 only if the value of %{$href-{$_[0]}} is equal to 'ND' for the whole 24 occurences. Any suggestions is greatly appreciated. Thank you, Chris sub site_offAir { for (values %{$href-{$_[0]}}) { return 1 if $_ eq 'ND'; #need to test all values are eq to 'ND' return if $_ ne 'ND'; } return ''; return 1; } -- Just my 0.0002 million dollars worth, Shawn Programming is as much about organization and communication as it is about coding. _Perl links_ official site : http://www.perl.org/ beginners' help : http://learn.perl.org/faq/beginners.html advance help: http://perlmonks.org/ documentation : http://perldoc.perl.org/ news: http://perlsphere.net/ repository : http://www.cpan.org/ blog: http://blogs.perl.org/ regional groups : http://www.pm.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: subroutine returning data
On 12-06-04 02:01 PM, Bill Stephenson wrote: The values %{$href-{$_[0]}} code is pretty ugly but I get it now. And it make sense to break out of the loop as soon as you don't pass the test. sub site_offAir { my $site_id = shift @_; for my $activity_code ( values %{ $activity_of-{$site_id} } ){ return if $activity_code ne 'ND'; } return 1; } -- Just my 0.0002 million dollars worth, Shawn Programming is as much about organization and communication as it is about coding. _Perl links_ official site : http://www.perl.org/ beginners' help : http://learn.perl.org/faq/beginners.html advance help: http://perlmonks.org/ documentation : http://perldoc.perl.org/ news: http://perlsphere.net/ repository : http://www.cpan.org/ blog: http://blogs.perl.org/ regional groups : http://www.pm.org/ -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: subroutine returning data
Thank you everyone. Your help has been very helpful.. Chris -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: subroutine returning data
Chris Stinemetz wrote: I have a subroutine that I want to return 1 only if the value of %{$href-{$_[0]}} is equal to 'ND' for the whole 24 occurences. One way to do it: sub site_offAir { return values %{ $href-{ $_[ 0 ] } } == grep( $_ eq 'ND', values %{ $href-{ $_[ 0 ] } } ) ? 1 : ''; } John -- Any intelligent fool can make things bigger and more complex... It takes a touch of genius - and a lot of courage to move in the opposite direction. -- Albert Einstein -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/