Sedat Yıldız avatar

Most Used Functions in Perl5 Lists #5

sdtyldz

Published: 23 Feb 2018 › Updated: 23 Feb 2018Most Used Functions in Perl5 Lists #5

Most Used Functions in Perl5 Lists #5

What Will I Learn?

Write here briefly the details of what the user is going to learn in a bullet list.

  • push() and pop() Stack Functions
  • unshift() and shift() Functions
  • splice() Function
  • join() Function

Requirements

Write here a bullet list of the requirements for the user in order to follow this tutorial.

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

Difficulty

Either choose between the following options:

  • Intermediate

push() and pop() Stack Functions

The push() and pop() functions will make your life easier if you are using a "last in first out" type list in your program.
The push() function is the easiest way to add an element after a listen.
Similarly, the pop() function is the easiest way to extract the last element of a listen:

@a = (1,2,3,4,5);                                             # if
push(@a, 6);                                                     # at the end of the transaction,
@a -> (1,2,3,4,5,6)                                        # it will be;
$x = pop(@a);                                                 # at the end of the transaction
$x -> 6 ve @a -> (1,2,3,4,5)                      # it will be.

In a similar way:

@a = (1,2,3,4,5);                                           # if
push(@a,6,7,8);                                           # at the end of the transaction,
@a -> (1,2,3,4,5,6,7,8,9)                          # it will be.

If you want to get an element at the end of an empty listen, undef will return; so

@a = ( ) ;                                                       # if
$x = pop(@)                                              # at the end of the transaction,
$x -> undef                                               # it will be.

unshift() and shift() Functions

To add an element per listenin unshift(); The shift() functions are used to retrieve and retrieve the leading element.

@a = (1,2,3,4,5);                                             # if
unshift(@a, 6);                                                # at the end of the transaction,
@a -> (6,1,2,3,4,5)                                       # it will be;
$x = shift(@a);                                               # at the end of the transaction,
$x -> 6 ve @a -> (1,2,3,4,5)                    # it will be.

In a similar way:

@a = (1,2,3,4,5);                                             # if
unshift(@a,6,7,8);                                         # at the end of the transaction,
@a -> (6,7,8,1,2,3,4,5)                                # it will be.

If you want to remove an element from the beginning of an empty list, undef will return; so,

@a = ( );                                                       # if
$x = shift(@a)                                        # at the end of the transaction,
$x -> undef                                             # it will be.

splice() Function

A function used to retrieve and retrieve slices from within lists. The general form is one of the following:

splice(@liste, $initial_position);
splice (@liste, $initial_position, $number);
splice (@liste, $initial_position, $number, @replacement_list);

If only two parameters (list@list and $initial_position) are used; splice () extracts the part of the list specified in the first parameter, starting from the index specified in the second parameter, up to the end, and returns this fragment as the result of the operation.

If used with three parameters, the third parameter specifies how many elements to remove from the starting position.

Finally, the fourth parameter, if present, specifies another list containing elements to be replaced in place of the removed elements.

I think the best way to describe this function would be to give an example for every possible situation ...

@a = (1,2,3,4,5,6); if
@b = splice(@a,2); after the statement @b->(3,4,5,6) it will be, and @a->(1,2) it will remain.

@a = (1,2,3,4,5,6); if
@b = splice(@a,0); after the statement  @b->(1,2,3,4,5,6) it will be, and @a->( ) it will remain.

@a = (1,2,3,4,5,6); if
@b = splice(@a,25); after the statement @b->() it will be, and @a->(1,2,3,4,5,6) it will remain.

@a = (1,2,3,4,5,6); if
@b = splice(@a,2,2); after the statement @b->(3,4) it will be, and @a->(1,2,5,6) it will remain.

@a = (1,2,3,4,5,6); and
@x = ("a", "b"); if
@b = splice(@a,2,1,@x); after the statement @b->(3) and @a->(1,2,"a","b",4,5,6) it will remain.

join() Function

A function that combines the elements within lists into a single character array.

General form:

join($concatenation_character, @list);

It shaped.

For example:

@a = (1,2,3,"a","ab","def");                                  # if
$x = join(:, @a);                                                   # after the statement
$x->"1:2:3:a:ab:def"                                           # the @a list does not change.
$x = join("", @a);                                                    #  after the statement
$x -> "123aabdef"                                                # the @a list does not change.

The fifth post of our series is over, thank you for reading. See you at the next tutorial.

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

Leave Most Used Functions in Perl5 Lists #5 to:

Written by

Her Şey Hakkında Hiçbir Şey

Read more #utopian-io posts


Best Posts From Sedat Yıldız

We have not curated any of sdtyldz's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From Sedat Yıldız