Perl5 List Processing and Elements #4

in #utopian-io7 years ago (edited)

What Will I Learn?

  • List Operations
  • List Elements
  • split() Function
  • sort() Function
  • Numeric Order

Requirements

  • Terminal or SSH
  • Linux/Unix Operating System or Linux Hosting

Difficulty

  • Intermediate

List Operations

You know Perl is very powerful in listing operations. Let's do a few examples together.

$a = 1;
$b = 2;
# The following statement uses two lists, but the lists have no names.
($a, $b) = ($b, $a);
# The values of $a and $b variables are replaced. That is, $a=2 and $b=1.

You will write in your programs many times, you will need to add elements to lists or remove elements from lists.

@a = (@a,"new");
# At the end of the @a list, an element with the value "new" is added.
@a = ("new",@a);
# The @a list is preceded by an element with the value "new".
@n = (1, 2, 3, 4);
($x,@n) = @n;
# $x=1 and @n = (2, 3, 4) it will be.
($x,$y,$n) = @n;
# $x=2, $y=3 and @n = (4) it will be.

If you need lists of consecutive numbers, you do not need to write each one individually.

@a = (1..6);
# @a = (1,2,3,4,5,6); is the same as.

The logic of your program is that you can assign a numeric variable to the list if you need a number of elements in a list, so you can use the list in a numeric context.

@n = (1..9);
$n = @a;
# $n=9 it will be.
@a = (a..z); print @a*2;
# displayed on screen 52. (52=26*2)

Watch out for the braces!

($n) = @a;
# $n, The first element of @a is 1.

Actually, I do not need to explain, but...

@a = b = (1...9);
# Both @a and @b (1,2,3,4,5,6,7,8,9)  it will be.

List Elements

The purpose of using a list is to access the elements of the listener in a specific order as needed. In Perl, list elements are addressed with indexes starting from zero, and indices are indicated between square brackets.

@a = (1..9); if
$a[0] -> 1, $a[1] -> 2 ... $a[8] -> 9 it will be.

If you have noticed, I did not say @a[1] -> 2. In fact, although $a[1] and @a[1] are interpreted as equivalent in Perl interpreters in the 5th and later versions currently used, be sure to put a dollar sign ($) at the beginning of the list name while addressing a single element of a list.

When talking about list elements one by one, the name of the listen must be used like a numeric variable; that is, a $ sign must be placed at the beginning of the list name when referring to a single element of a list.

@a = (1..9); if
$a[0] = 4; then @a = (5, 2, 3, 4, 5, ...,9) it will be.
$a[0]++; then @a = (6, 2, 3, 4, 5, ...,9) it will be.

When element slices are accessed through the lists; for example @a [0,2,1] in the first 3 elements of list @a should be preceded by @. To give an example of a list slice:

@b = ();
@a = (1..9); ise
@b[0,1] = @a[1,2]; after the statement
@b -> (2, 3) it will be.

@a[1,2] because the two element slices taken from @a are actually a list.

List indices can be numeric variables or any expression that is numeric as a result.

$x = 3;
@a = (1..9); if
$a[2 * $x] -> 7 it will be.

If the calculated index is not an integer, the nearest integer smaller than the number calculated as the index is considered as the index.

$x = 3;
@a = (1..9); if
$a[x / 2] -> 2 it will be.

Non-zero index values evaluate the indices of the elements starting from the sphere.

E.G.:

$a[-1]
# The first (last) element of the list @a list,
$a[-3]
# It addresses the third element of the @a list.
$x = 3;
@a = (1..9); if
$a[2 - $x] -> $a[-1] -> 9 it will be.

If an element is assigned beyond the length of a listener, the missing elements are filled with as many undef values as necessary and the value assigned to the element whose index is determined is placed.

@a = (1,2,3); if
$a[5] = "NEW"; after the statement;
@a -> (1, 2, 3, undef, undef, "NEW") it will be.

If a character string is used as an index, the addressed list element will vary according to the numeric value of the character string in question. If the character string is a numerically significant sequence (such as "123"), the numeric value of the character string is used as the index value. If the character string does not have a numeric meaning (such as "ax23"), the index takes the value zero. (This already suits the convention used to convert numeric values to Perl character strings.)

@a = (1,2,3); if;
$a["1"] -> 2 and
$a["a1"] -> $a[0] -> 1 it will be.

Creating a List by Splitting an Array: split() Function

The split() function is a very useful function that is very often used when developing web applications with Perl. For example, if you have a string of characters with names separated by commas, ie:

$s = "john,emma,david"; if
@name = split (",", $s);

command
It will evaluate each name separated by a comma in the $s array and create a list of these as names; that is, in the split() function

@names -> ("john", "emma", "david")

it will be.

List Sort by: sort() Function

The sort() function evaluates the list presented to itself as a parameter, evaluates the elements as a character array, and places them in ascending order:

@unordered= (1,12,2,21,3,13); if,
@in-line= sort(@unordered); after the statement
@in-line-> (1,12,13,2,21,3) it will be.

No, there is no printing error in the above line! Perl should pay more attention to character strings than numbers, so that the numeric values in the list are evaluated as character strings and so on. There is a way to tell Perl about the facts of life are not like that.

Numeric Order

"Behold Perl was so strong, he could not even line up a numerical list." I heard you say. I'm afraid you're not right. Yes Perl is strong; so powerful that the sort() function allows you to sort the criteria even if you do not. If you do not give a sorting criterion, as you know, it evaluates everything as a character string.

If you want numeric sorting;

@in-line = sort { $a <=> $b } (@unordered); or
@in-line = sort { $a <=> $b } @unordered;

command is sufficient.

The <=> operator compares the values on the two sides (the numeric values compare numerically, and the character string values compare as character strings).

"1" if the first is greater than the second;
"-1" if the first is smaller than the second;
Returns the value "0" if both are equal.

The $a and $b variables in the {$a <=> $b} operator for numeric sorting are not randomly chosen variable names. These variables must always be $a and $b. If you have $a and $b variables used in your program for other purposes, do not worry, the $a and $b values will not change.

The fourth issue of our series is here completed. See you next lesson time.

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Ustadım linux ile ilgili pek bilgim yok ama sayenizde bir şeyler öğrenmeye başladım. Teşekkür ederim.

Ben teşekkür ederim okuma zahmetinde bulunduğunuz için :)

assigning numerical variables to the list is a program I need to re-learn. thank you for experiencing information @sedatyildiz

I thank you for having trouble reading :)

Hey @sedatyildiz I am @utopian-io. I have just upvoted you!

Achievements

  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.15
TRX 0.15
JST 0.028
BTC 54034.48
ETH 2262.26
USDT 1.00
SBD 2.31