In Perl, arrays and hashes are essential data structures that allow you to group and manage collections of data efficiently.
Arrays in Perl are ordered lists that can store scalars (numbers, strings, etc.). You can use various functions to manipulate arrays.
# Declaring an array
@fruits = ("apple", "banana", "cherry");
# Accessing an element
print $fruits[1]; # Outputs: banana
# Adding an element
push(@fruits, "orange");
# Looping through the array
foreach my $fruit (@fruits) {
print $fruit;
}
Hashes are key-value pairs, which allow for easy data retrieval using unique keys. Hashes are unordered collections.
# Declaring a hash
%ages = ("Alice" => 30, "Bob" => 25, "Charlie" => 35);
# Accessing a value
print $ages{"Bob"}; # Outputs: 25
# Adding a key-value pair
$ages{"David"} = 20;
# Looping through the hash
while (my ($name, $age) = each %ages) {
print "$name is $age years old";
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?