YouTip LogoYouTip

Perl Arrays

A Perl array is a list variable that stores scalar values, and the variables can be of different types. Array variables start with @. To access array elements, use the format **$ + variable name + **, as shown in the following example: ## Example #!/usr/bin/perl @hits = (25, 30, 40); @names = ("google", "tutorial", "taobao"); print "$hits = $hitsn"; print "$hits = $hitsn"; print "$hits = $hitsn"; print "$names = $namesn"; print "$names = $namesn"; print "$names = $namesn"; In the program, the $ symbol is escaped with to output it as-is. Executing the above program produces the following result: $hits = 25 $hits = 30 $hits = 40 $names = google $names = tutorial $names = taobao * * * ## Creating Arrays Array variables start with the **@** symbol, elements are placed within parentheses, and arrays can also be defined using **qw**. @array = (1, 2, 'Hello'); @array = qw/This is an array/; The second array uses the **qw//** operator, which returns a list of strings with array elements separated by spaces. Of course, you can also define arrays across multiple lines: @days = qw/google taobao ... tutorial/; You can also assign values to arrays by index, as follows: $array = 'Monday'; ... $array = 'Sunday'; * * * ## Accessing Array Elements To access array elements, use the format **$ + variable name + **, as shown in the following example: ## Example @sites = qw/google taobao tutorial/; print "$sitesn"; print "$sitesn"; print "$sitesn"; print "$sitesn"; Executing the above program produces the following result: google taobao tutorial tutorial Array indices start from 0, meaning 0 is the first element, 1 is the second element, and so on. Negative indices read from the end, where -1 is the first element and -2 is the second element. * * * ## Array Sequences Perl provides a way to output arrays in sequence form, with the format **start value + .. + end value**, as shown in the following example: ## Example #!/usr/bin/perl @var_10 = (1..10); @var_20 = (10..20); @var_abc = ('a'..'z'); print "@var_10n"; print "@var_20n"; print "@var_abcn"; Executing the above program produces the following result: 1 2 3 4 5 6 7 8 9 10 10 11 12 13 14 15 16 17 18 19 20 a b c d e f g h i j k l m n o p q r s t u v w x y z * * * ## Array Size The array size is determined by the scalar context of the array: ## Example @array = (1,2,3); print "Array size: ", scalar @array, "n"; Executing the above program produces the following result: Array size: 3 The array length returns the physical size of the array, not the number of elements. We can see this in the following example: ## Example #!/uer/bin/perl @array = (1,2,3); $array = 4; $size = @array; $max_index = $#array; print "Array size: $sizen"; print "Max index: $max_indexn"; Executing the above program produces the following result: Array size: 51 Max index: 50 As you can see from the output, there are only four array elements, but the array size is 51. * * * ## Adding and Removing Array Elements Perl provides some useful functions for adding and removing array elements. If you don't have programming experience, you might wonder what a function is. Actually, the **print** we used earlier is an output function. The following table lists commonly used array operation functions: | No. | Type and Description | | --- | --- | | 1 | **push @ARRAY, LIST** Appends a list of values to the end of an array | | 2 | **pop @ARRAY** Removes the last value from an array | | 3 | **shift @ARRAY** Pops the first value from an array and returns it. The array indices also decrease by one. | | 4 | **unshift @ARRAY, LIST** Prepends a list to the beginning of an array and returns the new number of elements. | ## Example #!/usr/bin/perl @sites = ("google","tutorial","taobao"); $new_size = @sites ; print "1. @sites = @sitesn" . "Original array length: $new_sizen"; $new_size = push(@sites, "baidu");
← Perl If Else StatementPerl Syntax β†’