The previous article lists the similarities and differences between the ways of objects and JS in Perl. This continues to complement the array, the hash of the associated operation.
One, array
You can add additions and deletions to the array. Unlike JS is that these functions are global, JS is hanging on the array.prototype.
1, the operation of the tail of the array POPs (delete the last element), push (added at the tail)
@goods = Qw/pen pencil/;
Pop (@goods); # @goods into (pen)
push (@goods, ' brush '), # @goods changed (pen, brush)
In Perl, the parentheses are optional (depending on the context) of the function call, just like the print used earlier. The following are equivalent
Pop @goods; # @goods into pen
push @goods, ' brush '; # @goods changed (pen, brush)
2, operation of the array header shift (delete first element), Unshift (add element in header)
3, remove or insert splice anywhere
4, reverse array, Perl has the reverse function, JS has no corresponding function.
5, sorted array Sort,perl and JS are available.
The function mentioned by 2,3,4,5 does not stick to the demo code.
6,js use the Length property to get an array of lengths, unlike Perl, there are 3 ways to get
@goods = Qw/pen pencil/;
# Assign the array variable to a scalar variable
$len = @goods;
# Use the scalar function
$len = scalar (@goods);
# The index of the last element plus 1
$len = $ #goods + 1;
7, traversing the array, perl with the Foreach function
@goods = Qw/pen pencil brush/;
# The default $_
foreach (@goods) {
print ' $_ '. \ n ";
}
# custom Variable
foreach $item (@goods) {
print $item. " \ n ";
}
ES5 can use foreach
[Pen ', ' pencil ', ' brush '].foreach (function (item) {
Console.log (item)
})
Two, hash
1, get keys and values
%person = (
name => ' Jack ', age
=>
);
@k = Keys%person; # (name, age)
@v = values $person; # (' Jack ', 30)
ES5 has Object.keys, but no object.values.
person = {
name: ' Jack ',
age:30
}
Object.keys (person)//[' Name ', ' age ']
2, get the number of key-value pairs (key-value) (Easy for Perl)
%person = (
name => ' Jack ', age
=>
);
$len = Keys%person; # 2
For JS, you may need a for-in whole object
function Getobjlen (obj) {
var len = 0 for
(var a in obj) {
if (Obj.hasownproperty (a))
len++
}
return len
}
var person = {
name: ' Jack ',
age:30
}
Getobjlen (person)//2
3, traversing the object
There are two ways to Perl, a While+each, a fetch of the keys, and a foreach.
%person = (
name => ' Jack ', age
=>
);
# mode 1
while (($k, $v) = each%person) {
print "$k: $v". " \ n ";
}
# mode 2
@keys = keys%person;
foreach (@keys) {
print "$_:". $person {$_}. " \ n ";
}
JS a for in can.
4, to determine whether a key exists, Perl with the EXISTS function
%person = (
name => ' Jack ', age
=>
);
If (exists $person {ndame}) {
print ' yes ';
} else {
print ' no ';
}
JS with the in operator.
5, delete key, all with delete, but Perl is a function, JS is an operator
%person = (
name => ' Jack ', age
=>
);
Delete $person {' name '};
Three, array and hash interchange
Perl Rihashi can easily be transferred to a group
%person = (
name => ' Jack ', age
=>
);
@arr =%person; # Converts a hash into an array (' name ', ' Jack ', ' age ', 30)
The array is turned into a hash
@nums = Qw/zero 0 One 1 two 2/;
%hash = @nums;
while ($k, $v) = each%hash) {
print "$k: $v \ n";
}
Print as follows
The above interchange JS does not have native support, you need to realize.
The above comparison of Perl and JS (array, hash) is a small series to share all the content, I hope to give you a reference, but also hope that we support the cloud habitat community.