Re: What is this called: ($myvar-{otherid}) ? 'stringA' : 'stringB';
Jeff Kowalczyk wrote: I'm not yet able to read certain parts of perl code. What is this comparison/alternation after the hash lookup on 'otherid' called, and what does the code do? $myvar-{id} = ($myvar-{otherid}) ? 'stringA' : 'stringB'; Thanks. That is the conditional operator. Joseph -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
What is this called: ($myvar-{otherid}) ? 'stringA' : 'stringB';
I'm not yet able to read certain parts of perl code. What is this comparison/alternation after the hash lookup on 'otherid' called, and what does the code do? $myvar-{id} = ($myvar-{otherid}) ? 'stringA' : 'stringB'; Thanks. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: What is this called: ($myvar-{otherid}) ? 'stringA' : 'stringB';
I'm not yet able to read certain parts of perl code. What is this comparison/alternation after the hash lookup on 'otherid' called, and what does the code do? $myvar-{id} = ($myvar-{otherid}) ? 'stringA' : 'stringB'; It's a ternary or flip-flop statement. In this case, the above is the equivalent of: if myvar-otherid is set, then set myvar-id to stringA, otherwise, set it to stringB. Code-wise, the above is equivalent to: if ($myvar-{otherid}) { $myvar-{id} == 'stringA' }; else { $myvar-{id} == 'stringB'; } -- Morbus Iff ( i put the demon back in codemonkey ) Culture: http://www.disobey.com/ and http://www.gamegrene.com/ My book, Spidering Hacks: http://amazon.com/exec/obidos/ASIN/0596005776/ icq: 2927491 / aim: akaMorbus / yahoo: morbus_iff / jabber.org: morbus -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: What is this called: ($myvar-{otherid}) ? 'stringA' : 'stringB';
On Thu, 2003-11-20 at 15:18, Jeff Kowalczyk wrote: I'm not yet able to read certain parts of perl code. What is this comparison/alternation after the hash lookup on 'otherid' called, and what does the code do? $myvar-{id} = ($myvar-{otherid}) ? 'stringA' : 'stringB'; Thanks. Jeff, This is the if-then-else operator (for lack of a better term). The code above could be translated to: if ($myvar-{otherid}) { $myvar-{id} = 'stringA'; } else { $myvar-{id} = 'stringB'; } From 'perldoc perlop': Ternary ?: is the conditional operator, just as in C. It works much like an if-then-else. If the argument before the ? is true, the argu- ment before the : is returned, otherwise the argument after the : is returned. For example: printf I have %d dog%s.\n, $n, ($n == 1) ? '' : s HTH, Kevin -- Kevin Old [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: What is this called: ($myvar-{otherid}) ? 'stringA' : 'stringB';
On Thursday, Nov 20, 2003, at 12:18 US/Pacific, Jeff Kowalczyk wrote: I'm not yet able to read certain parts of perl code. What is this comparison/alternation after the hash lookup on 'otherid' called, and what does the code do? $myvar-{id} = ($myvar-{otherid}) ? 'stringA' : 'stringB'; it is short for if ($myvar-{otherid}) { $myvar-{id} = 'stringA'; }else{ $myvar-{id} = 'stringB'; } so you too might like Data::Dumper and go with a short script like: #!/usr/bin/perl -w use strict; use Data::Dumper; # #FILENAME# - is for my $myvar = { otherid = 'bob', id = 'alice' }; print Dumper $myvar ; $myvar-{id} = ($myvar-{otherid}) ? 'stringA' : 'stringB'; print Dumper $myvar ; delete $myvar-{otherid}; $myvar-{id} = ($myvar-{otherid}) ? 'stringA' : 'stringB'; print Dumper $myvar ; which generates $VAR1 = { 'otherid' = 'bob', 'id' = 'alice' }; $VAR1 = { 'otherid' = 'bob', 'id' = 'stringA' }; $VAR1 = { 'id' = 'stringB' }; ciao drieux --- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: What is this called: ($myvar-{otherid}) ? 'stringA' : 'stringB';
Jeff Kowalczyk wrote: I'm not yet able to read certain parts of perl code. What is this comparison/alternation after the hash lookup on 'otherid' called, and what does the code do? $myvar-{id} = ($myvar-{otherid}) ? 'stringA' : 'stringB'; perldoc perlop [snip] Conditional Operator Ternary ?: is the conditional operator, just as in C. It works much like an if-then-else. If the argument before the ? is true, the argument before the : is returned, otherwise the argument after the : is returned. John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]