Perl Programming( The Beginners Guide )

Perl programming is the most widely used language in information technology, it was developed by Rasmus Lerd-Sveiby, a Norwegian computer programmer, and it first began to be produced by AT&T. Its syntax is similar to C and many other languages, and its support for dynamic typing. It includes many libraries like CGI and has several packages that support data processing, as well as web development and server-side scripting. The main purpose of Perl is to allow easy communication between different programs. There are many differences in terms of syntax, which are often overlooked. But as you see it is not too difficult to understand. Here I will show you some examples of using our Perl scripts. You can also download our free samples, test them on your machine, etc.

Our basic example:

This is a very simple example. This program uses only the standard library module system, but it doesn’t have anything else. Let’s say we have to print all the words in this file “my_words.txt”. First, let’s do the following:

A) Write the whole line and then end with $

B) Run the above command

 

1)  After writing those two lines, we could run these commands:

a) Start the whole file

b) If you see any errors in our code, you need to add them

c) Once we finish the file, close it.

 

2)   If we make changes to our code, we need to run these steps again:

 

a) Copy the file “my_words.txt” to your computer. Then open your IDE (or Editor) and make the necessary changes to your files. Finally, save the change.

b) Remember our codes, so we can use them and execute them again in our IDE or Editor.

 

3)  Now we’re ready to print our words. We just write a few more lines as follows:

 Now we’re done! At this point you might feel, why use Perl if we can get by with C & Vb? The reason is that you can develop such huge projects with C & Vb, but Perl is much simpler, and it has the advantage of being faster than what we have now. So let's go and try another very simple task: printing the number of times a word appears in the sentence. In this case, we want to count the number of times a given word appears in an array of words. To do it we can use something called string manipulation. We’ll start off by creating a word_counter variable. After that, the next thing we have to do is to create an empty array of words. The variable will be containing both the word and their counts.

 

4)  After defining the variables, first let’s implement our problem. Our aim is to count the amount of times a certain word appears in the array of words. What would happen if we change the way we count the words. For instance, one word can change place twice in our array. That means that if we change the number of times the word appears in our array, we’d have the same number as before.

 

5)  So when do we add a new word to our array? When it appears. To add a new word to our array we have to call add_word() function from array_length. A word can appear once or more than twice in our array. Thus to count how many times there are more than two words in our array, we’d have to multiply by 2.

 

6)  In practice, the above solution works perfectly for us. With this code, you can print out the number of times each word appears in our document. And we’ll never forget about the keyword “|”. Why we use |? Because it switches between the variables in our array which will be defined in our array. If you use the | operator you switch the order of variables. The other operators are the same for every word. If you’re trying to calculate a certain word’s number of appearances, you’d just have to write your own addition/subtraction/multiplication. For example:

 

7)  If you want to see all the variables defined in your arrays, you can change the display mode. We have to make sure that the variable “word_counter” is assigned the appropriate name. If everything looks OK, we can print out all the numbers that we have added to our array and then move on.

 

8)  On the second line, we have introduced an inner loop to count the different ways a word occurs in our array. To add more variables to our array, we call an outer loop. In the outer loop, you need to add more variables to your array. By the way, you can print their counts here.

The final result will look like this:

 

9)  What will be the total amount of words in this file “my_words.txt”? The answer is in your mind. Your brain does it automatically, and it’ll generate whatever you give it to. Lets sum up how many times one word occurs in our array, that should give us the number of times: 21. There are 21 different ways a word appears in our document. The variable is named word_counter. As mentioned previously your mouse click should start counting times.

This is only a small example of what is possible with Perl. More complex operations can be done if you know the right tools.

 

Comments

Popular posts from this blog

Robotics ( Explain, Applications or its Types, AI robotics, Control, In Engineering learn robotics programming )

AI Robotics: Some world's most popular AI robots are here