Saturday, March 21, 2020

A Beginner Guide to Comparing Values in Perl

A Beginner Guide to Comparing Values in Perl Perl  comparison operators can sometimes be confusing to new Perl programmers. The confusion stems from the fact that  Perl  actually has two sets of comparison operators - one for comparing numeric values and one for comparing string American Standard Code for Information Interchange (ASCII) values.   Since  comparison operators  are typically used to control logical program flow and make important decisions, using the wrong operator for the value you are testing can lead to bizarre errors and hours of debugging, if youre not careful. Dont forget to catch whats written at the very bottom of this page for some last-minute things to remember. Equal, Not Equal The simplest and probably most used comparison operators test to see if one value is equal to another value. If the values are equal, the test returns true, and if the values are not equal, the test returns false. For testing the equality of two numeric values, we use the comparison operator . For testing the equality of two string values, we use the comparison operator eq (EQual). Heres an example of both: if (5 5) { print for numeric values\n; } if (moe eq moe) { print eq (EQual) for string values\n; } Testing for the opposite, not equal, is very similar. Remember that this test will return true if the values tested are not equal to each other. To see if two numeric values are not equal to each other, we use the comparison operator !. To see if two string values are not equal to each other, we use the comparison operator ne (Not Equal). if (5 ! 6) { print ! for numeric values\n; } if (moe ne curly) { print ne (Not Equal) for string values\n; } Greater Than, Greater Than or Equal To Now lets look at the  greater than  comparison operators. Using this first operator, you can test to see if one value is  greater than  another value. To see if two  numeric  values are  greater than  each other, we use the comparison operator  . To see if two  string  values are  greater than  each other, we use the comparison operator  gt  (Greater Than). if (5 4) { print for numeric values\n; } if (B gt A) { print gt (Greater Than) for string values\n; } You can also test for  greater than or equal to, which looks very similar. Keep in mind that this test will return  true  if the values tested are equal to each other, or if the value on the left is greater than the value on the right. To see if two  numeric  values are  greater than or equal to  each other, we use the comparison operator  . To see if two  string  values are  greater than or equal to  each other, we use the comparison operator  ge  (Greater-than Equal-to). if (5 5) { print for numeric values\n; } if (B ge A) { print ge (Greater-than Equal-to) for string values\n; } Less Than, Less Than or Equal To There are a variety of comparison operators you can use to determine the logical flow of your  Perl programs. Weve already discussed the difference between the Perl numeric comparison operators and the Perl string comparison operators, which can cause some confusion to  new Perl programmers.  Weve also learned how to tell if two values are equal to, or not equal to each other, and weve learned how to tell if two values are greater than or equal to each other. Lets look at the  less than  comparison operators. Using this first operator, you can test to see if one value is  less than  another value. To see if two  numeric  values are  less than  each other, we use the comparison operator  . To see if two  string  values are  less than  each other, we use the comparison operator  lt  (Less Than). if (4 5) { print for numeric values\n; } if (A lt B) { print lt (Less Than) for string values\n; } You can also test for,  less than or equal to, which looks very similar. Remember that this test will return  true  if the values tested are equal to each other, or if the value on the left is less than the value on the right. To see if two  numeric  values are  less than or equal to  each other, we use the comparison operator  . To see if two  string  values are  less than or equal to  each other, we use the comparison operator  le  (Less-than Equal-to). if (5 5) { print for numeric values\n; } if (A le B) { print le (Less-than Equal-to) for string values\n; } More Information on Comparison Operators When we talk about string values being equal to each other, were referring to their ASCII values. So, the capital letters are technically less than the lowercase letters, and the higher the letter is in the alphabet, the higher the ASCII value. Make sure you check your  ASCII values  if youre trying to make logical decisions based on strings.

Thursday, March 5, 2020

The Importance of Government Explained

The Importance of Government Explained John Lennons  Imagine is a beautiful song, but when he tallies up the  things he can imagine us living without - possessions, religion and so on - he never asks us to imagine a world without government. The closest he comes is when he asks us to imagine that there are no countries, but thats not exactly the same thing. This is probably because Lennon was a student of human nature. He knew that government might be one thing we cant do without. Governments are important structures. Lets imagine a world with no government. A World Without Laws   Im typing this on my MacBook right now. Lets imagine that a very large man - well call him Biff - has decided that he doesnt  especially like my writing. He walks in, throws the MacBook to the floor, stomps it into little pieces, and leaves. But before leaving, Biff tells me that if I write anything else he doesnt like, hell do to me what he did to my MacBook.Biff just established something very much like his own government. It has become against Biffs law for me to write things that Biff doesnt like. The penalty is severe and enforcement is fairly certain. Whos going to stop him? Certainly not me. Im smaller and less violent than he is.But Biff isnt really the biggest problem in this no-government world. The real problem is a greedy, heavily armed guy - well call him Frank - who has learned that if he steals money then hires enough muscle with his ill-gotten gains, he can demand goods and services from every business in town. He can take anything he wants and make almost anybo dy do whatever he demands. Theres no authority higher than Frank that can make him stop what hes doing, so this jerk has literally created his own government - what political theorists refer to as a despotism, a government ruled by a despot, which is essentially another word for tyrant. A World of Despotic Governments   Some governments arent much different from the despotism I just described. Kim Jong-il technically inherited his army instead of hiring it in North Korea, but the principle is the same. What Kim Jong-il wants, Kim Jong-il gets. Its the same system Frank used, but on a larger scale. If we dont want Frank or Kim Jong-il in charge, we must all get together and agree to do something to prevent them from taking over. And that agreement itself is a government. We need governments to protect us from other, worse power structures that would otherwise form in our midst and deprive us of our rights. As Thomas Jefferson said  the Declaration of Independence:   We hold these truths to be self-evident, that all men are created equal, that they are endowed by their Creator with certain unalienable rights, that among these are life, liberty and the pursuit of happiness. That ​to secure these rights, governments are instituted among men, deriving their just powers from the consent of the governed, that whenever any form of government becomes destructive of these ends, it is the right of the people to alter or to abolish it, and to institute new government, laying its foundation on such principles and organizing its powers in such form, as to them shall seem most likely to affect their safety and happiness.