Learn the relation between Arrays and Strings in Perl Programming

0
1139
Perl (10) - Arrays (2)

Perl (10) - Arrays (2)

Welcome to part two of the Arrays topic. In this article, we are going to talk about the relation between Arrays and Strings. In Perl, an array could be easily converted to a string, and vice versa. A short, yet interesting topic, so don’t miss it!!

Converting Strings to Arrays
A string can be converted into an array using the split() function.

Syntax

Where:
@ARRAY is the array variable that will be assigned the resulting array.
$STRING is the string to be splitted.
REGEX is the string (pattern) to be matched, and to split the $STRING accordingly.

Example
In this example, we are going to write a script that prompts the user to enter a line of text, and then splits that line into words. The individual words will be assigned to an array. Finally, the array elements will be printed, one per line.

When executed, the program should behave as follows:
1

The user input is accepted using the <stdin> operator, and then assigned to the $str variable. The chomp() function removes the trailing newline character from the input string. To extract the individual words from a line of text, the split() function will match the whitespaces (knowing that words in a sentence are separated by whitespaces). The resulting list of words will be assigned to the array variable @array. To loop on the list of elements in an array, the best way is to use the foreach loop (as we know from the previous article).

Extracting Individual Characters in a String
Using the split() function with empty search string “” returns the list of individual characters in a given string.

Example
Re-write the previous example to split the input string into characters (instead of words), and print the characters, one per line.

When executed, it will print the characters as required:
2

Similarly, the split() function can be used to process the /etc/passwd file (in UNIX and Linux systems). In this case, the search pattern will be /:/
I will leave this for the reader as an exercise.

Converting Arrays to Strings
The opposite operation could be done using the join() function.

Syntax

Where:
@ARRAY: is the input array.
$CONNECTING_STRING is the string to be used to connect the individual array elements.
$STRING is the resulting string.

Example
Having the array @cities that contains names for some world’s famous cities, we need to produce a string that contains all the array elements, separated by colon “:”

Executing this Perl script gives the following output:
3

Learn the Basics of C Programming Language

Concatenating Arrays
Two or more arrays could be concatenated simply using the following statement:

Example
Assume you have two arrays:

To combine the two arrays into a bigger one, use the following statement:

Printing the @numbers array, will produce:

Summary
In this article, we have looked into the relationship between arrays and strings, and how to convert one type to the other.

  • The split() function converts a string to an array.
  • The join() function converts an array into a string.

That was part two in the Arrays topic. See you in the next article.

LEAVE A REPLY

Please enter your comment!
Please enter your name here