The following is perl5's Reading Notes.
1. Why should I use the array elements of the list?
Assume that the following data is available:
Beijing, China
Tianjin, China
Tokyo, Japan
Seoul, South Korea
If it is stored in the list, you want:
China => Beijing, Tianjin
Japan => Tokyo
Korea => Seoul
In perl4, you can only treat the elements after => as strings. During each processing, you need to extract, sort, and then access the strings. for Perl, these operations should have been implemented internally.
2. Create a list with array elements
1) perl reference symbol "/"
You can use the symbol "/" to create a reference to an array, list, or common variable, as shown in
$ Aref = // @ array; # array reference
$ Href =/% list; # reference to the list
$ Scalar =/$ var; // The value
References can be passed:
$ Xy = $ Aref; # $ reference created by xy to @ Array
$ P [3] = $ href; # $ P [3] creates a reference to % list
$ Z = $ P [3]; # $ Z also creates a reference to % list
References are similar to pointers in C language. The difference is that references own variable types. For example:
Print $ Aref; the result is array (0x275d18)
Print $ href; the result is hash (0x1830ef4)
2) use [...] to create a reference to the unknown array, and use {key => value,...} to create a reference to the unknown list.
For example: $ Aref = [1, 2, "foo", UNDEF, 13];
$ Href = {key1 => val1, key2 => val2 };
The effect is as follows:
@ Array = (1, 2, "foo", UNDEF, 13 );
$ Aref = // @ array;
3) access objects with reference
For a sufficient number, you can use:
@ A equals @ {$ Aref}
Reverse @ a reverse @ {$ Aref}
$ A [3] $ {$ Aref} [3]
$ A [3] = 127; $ {$ Aref} [3] = 17
For hash tables, the situation is very similar
% H % {$ href}
Keys % H keys % {$ href}
$ H {'red' }$ {$ href} {'red '}
$ H {'red'} = 7 $ {$ href} {'red'} = 7
It can also be used wherever you want, such
For my $ elment (@ {$ Aref }){
}
Like a C pointer, Perl can also use "->" to reference its sub-objects, such
$ {$ Aref} [3] ==$ Aref-> [3]
$ {$ Href {red }==$ href-> {red}
You can also create multiple pseudo arrays.
@ A = ([, 3], [, 6], [, 9]);
$ A [n] is a reference to the Child array. You can use $ {$ A [n]} [N] or $ A [n]-> [N] to access the object.
It also supports mixed array and hash phenomena, such
@ A = ([1, 2, 3, 5],
{A => 'A', B => 'B '}
);
You can use $ A [0]-> [1] ($ {$ A [0]} [0]), $ A [1]-> {A} ($ {A [0]} {A}) Access
For a reference with more than two levels, "->" can be omitted. $ A [0]-> [1] can be written as $ A [0] [1], $ {$ A [0]} [1]} [2] can be written as $ A [0] [1] [2];
3. Specific solutions
Let's look at how the program we started should be written:
My % table;
Print "input lick/" city, country/". And end input press enter directly/N ";
While (<> ){
Chomp;
If ($ _ eq ") {last ;}
My ($ City, $ country) = Split/, [] */;
$ Table {$ country} = [] Unless exists $ table {$ country}; # create an empty anonymous List reference if it does not exist.
Push @ {$ table {$ country}, $ city;
}
# Output
Foreach $ country (sort keys % table ){
Print "$ country :";
My @ cities =@{$ table {$ country }};
Print join ',', sort @ cities;
Print "./N ";
}