So I just noticed something cool. As long as the logic's not too complex, you can use a ternary statement to replace a switch statement in Perl. I mean, you COULD use the Switch statement, but you'd have to have the Switch.pm module on the machine running the script. If you were to instead use a ternary operator, you could do the logic with nothing more than Perl's native language methods.
use strict;
use warnings;
my $testChoice;
my $selection;
$selection = shift;
$testChoice = ($selection == 1) ? "one" :
($selection == 2) ? "two" :
($selection == 3) ? "three" :
"invalid choice" ;
print $selection, ", ", $testChoice;
I could see such logic working particularly well for a menu system.
Edit: Shit, why hardcode a value for selection? You can shift and pass it through the command line when testing out the script running it. Doing so made me realize that you can test for multiple conditions with the right syntax.
use strict;
use warnings;
my $testChoice;
my $selection;
$selection = shift;
$testChoice = ($selection == 1 || $selection == 2) ? "one" :
($selection == 3 || $selection == 4) ? "two" :
($selection == 5 || $selection == 6) ? "three" :
"invalid choice" ;
print $selection, ", ", $testChoice, "\n";
Yeah, this works pretty slickly. In fact, I can show you some sample tests from my machine.
b-man@B-Man-Linux:~/Desktop$ perl test.pl 0
0, invalid choice
b-man@B-Man-Linux:~/Desktop$ perl test.pl 1
1, one
b-man@B-Man-Linux:~/Desktop$ perl test.pl 2
2, one
b-man@B-Man-Linux:~/Desktop$ perl test.pl 3
3, two
b-man@B-Man-Linux:~/Desktop$ perl test.pl 4
4, two
b-man@B-Man-Linux:~/Desktop$ perl test.pl 5
5, three
b-man@B-Man-Linux:~/Desktop$ perl test.pl 6
6, three
b-man@B-Man-Linux:~/Desktop$ perl test.pl 7
7, invalid choice
So you can take this logic one step further and do multiple variable assignment with your ternary statement.
use strict;
use warnings;
my $testChoice;
my $testChoice2;
my $selection;
$selection = shift;
($testChoice, $testChoice2) = ($selection == 1 || $selection == 2) ? ("one", "foo") :
($selection == 3 || $selection == 4) ? ("two", "bar") :
($selection == 5 || $selection == 6) ? ("three", "derp") :
"invalid choice" ;
print $selection, ", ", $testChoice, , ", ", $testChoice2, "\n";
When you run this, you get the following results:
b-man@B-Man-Linux:~/Desktop$ perl test.pl 1
1, one, foo
b-man@B-Man-Linux:~/Desktop$ perl test.pl 3
3, two, bar
b-man@B-Man-Linux:~/Desktop$ perl test.pl 5
5, three, derp
I'm not quite sure how far you can take this thinking in other languages because I haven't tested it. However, I now know that it works in Perl, and that's awesome.
@B-Man:
If you've gone this far, you've gone too far. :-p (http://us.thedailywtf.com/Articles/Macro_Polo.aspx)
I see the bug.
There's a big z in the last condition test, but it should be a little z.
I see your Perl and raise you a Java:
package blah;
public class StringChanger {
public static void main (String[] args) {
String test = "pONy 2012";
String upper = "";
String lower = "";
for (int i=0; i<test.length(); i++) {
char x = test.charAt(i);
lower += x >= 65 && x <= 90 ? (char)(x+32) : x;
upper += x >= 97 && x <= 122 ? (char)(x-32) : x;
}
System.out.println(upper + "\n" + lower);
}
}
Btw, can I ask what this part of your code does?
$uppercaseTest .= $character | " ";
$lowercaseTest .= $character & "_";
I get that it's appending the new strings with upper or lower-case characters, but what does the | " " and & "_" mean exactly?
Sure, I can explain that. To understand what those mean, you have to understand that | and & are not boolean operators in their current context, but bitwise operators. We're actually doing a bit of "binary math." The explanation is a bit long, so have some spoiler tags.
To really understand what's going on, you have to think of your characters not as characters, but as binary values. Remember that uppercase letters and lowercase letters are only different by one single bit.
While "A" is:
1000001
"a" is :
1100001
Now " " is:
0100000
With the first statement, we're doing a "or" bitwise operation. This means that only one of the values has to be a "1" in either input binary for the result to have a "1" at its corresponding location.
1000001 OR
0100000
-------
1100001
Note that if both input binary values are 1, the output binary value is still one, so any characters that are already lowercase are unaffected.
The second statement is an "and" bitwise operation. This means that BOTH input binary values have to be "1" for the result to be a "1". Now, "_" just so happens to be:
1011111
Notice how it's basically the inverse of " ", binary wise? Plugging it into an "and" bitwise operation with "a" as the first value would yield.
1100001 AND
1011111
-------
1000001
This allows all of the original letter bits to be retained, except the bit at the position that controls case. This operation makes that bit always equal zero. This effectively turns any lowercase characters into uppercase characters.
I hope that clears things up.
Also, " " and "_" can be written a bit less cryptically in hexidecimal as chr 0x20 and chr 0xDF, respectively. They can also be written in binary chr 0b010000 and chr 0b1011111, respectively.
$uppercaseTest .= $character | chr 0x20;
$lowercaseTest .= $character & chr 0xDF;
or
$uppercaseTest .= $character | chr 0b0100000;
$lowercaseTest .= $character & chr 0b1011111;
Haha, okay, I just realized another way of doing things. Instead of explicitely stating the inverse, you can use the unary "not" bitwise operator. You can use this on the hex or binary code,, or you can use decimal numbering to represent the flag value.
$uppercaseTest .= $character | chr 32;
$lowercaseTest .= $character & chr ~32;