Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What are the commonly used perl functions

Shulou Source: shulou.com Published: 2022-06-01 17:24:42 09月23日 Update

This article mainly shows you "what are the commonly used perl functions", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "what are the commonly used perl functions" this article.

Common perl functions:

Chop and chomp:

My $line = "hello\ n"

Chomp $line; # deletes the "\ n" at the end of $line ($/ specified)

Chop $line; # removes the last character of $line

Split and join: # cutting function, glue function

$str = "A:B:C"

My @ arr = split/:/,$str; # @ arr = qw (A B C)

$str= join (",", @ arr); # $str= "AMagi BMagee C"

Scalar returns the number of array elements

My @ arr = (1, 2, 3, 4)

Print scalar (@ arr); # shows 4

My $num = @ arr; print $num; # shows 4

$num = $# arr; prnt $num; # display 3

Length returns scalar length

My $len = length ("ATCGAA")

Print $len; # shows 6

String interception function: substr (scalar, starting point, length)

My $seq = "ATGTAA"

My $code = substr ($seq,0,3)

Print $code; # shows ATG

Substr ($seq,0,3) = "TTG"

Print $seq; # shows TTGTAA

Scalar or array reverse order: reverse function

My @ arr = (1, 2, 3, 4)

My @ rev = reverse @ arr; # @ rev = (4, 3, 2, 1)

My $str = "ATCG"

My $rev = reverse $str; # $rev = "GCTA"

Shift, pop, unshift, push functions

My @ arr = (1, 2, 3, 4)

Take the first element of the array:

My $first = shift @ arr; # @ arr:

Take the last element of the array:

My $last = pop @ arr; # @ arr: (2)

Add an element to the beginning of the array:

Unshift @ arr, 1; # @ arr: (1, 2, 3)

Add elements to the array:

Push @ arr, (4jue 5); # @ arr: (1pr 2pr 3jre 4pr 5)

Sorting arrays: sort function

Sort by numeric size: @ SORT = sort {$a $b} @ array

Sort in dictionary order: @ SORT = sort {$a cmp $b} @ array

# 1 sorts by default in dictionary order: @ SORT = sort @ array

# 2 sort from small to big by default

# 3 reverse sort: change the positions of $an and $b above

For example:

My @ arr = (2, 14, 14, 3)

My @ arr2 = sort @ arr

Print "@ arr2\ n"; # output: 14 2 3

@ arr2 = sort {$aqb} @ arr

Print "@ arr2\ n"; # output: 2 3 14

Keys and values functions

My% hash = (a = > 1, b = > 2, c = > 3)

My @ key = keys% hash; # @ key:

My @ value = values% hash; # @ value:

Print "keys = @ keys\ nvalues = @ value\ n"

Display:

Keys = c a b

Values = 3 1 2

Defined and exists functions

Defined: returns false if its parameter is not defined, or if the value is undef, otherwise it returns true

Exists: verify whether a key exists in hash, and return true if it exists, false otherwise

My $str

Defined $str; # returns false

$str = 1

Defined $str; # returns true

My% hash= (a = > 1m b = > 2m c = > 3)

Exist $hash {a}; # returns true

Exist $hash {d}; # returns false

$hash {d} = undef

Exist $hash {d}; # returns true

The above is all the contents of the article "what are the commonly used perl functions?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Functions arrays sorting elements commonly used content scalars articles reverse order dictionaries characters length order learning help output from small to large number from small position Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux vpn Huawei Shulou Tech Info Docker