Most Used Functions in Perl5 Lists #5

in #utopian-io7 years ago (edited)

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 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

Sort:  

Thanks for all the information

Thank you for taking the time and reading it.

Your contribution cannot be approved because it does not follow the Utopian Rules.
Utopian rule

  • Tutorials must be technical instructions that teach non-trivial aspects of an Open Source project.

Explanation

  • The tutorial is just so simple that can be considered trivial.Utopian needs more good quality work like how to use these functions to realize the complex project instead of simply introduting the concept of them
  • Do not add others tutorials to your Curriculum

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

I think you're a new moderator.
The topics I mentioned in Curriculum belong to my stolen account. Details here.
Perl is an open source software language.
The tutorial is not simple, it is as descriptive and necessary as the others. There is no similarity on the Internet.
To be able to examine a tutorial content, you should have an idea about that project.
In Utopian the purpose is not to reject the sender without hope. Please contact your supervisor and review again!