Reputation: 7471
I tried writing a subroutine that iterates through a list of strings and prints each string, but it doesn't work:
use HTTP::Date;
my @date_strings_array = ("Jun 1, 2026", "Aug 26, 2018 GMT-05:00", "Aug 26, 2018");
print_datetimes(@date_strings_array);
sub print_datetimes {
my @date_string_array = shift;
foreach $date_string (@date_string_array) {
print("The current iteration is $date_string.");
}
}
It only prints the first iteration:
$ perl /example/test.pl
The current iteration is Jun 1, 2026.
Why does this only print the first item in the array?
Upvotes: 0
Views: 53
Reputation: 242363
shift only retrieves one element. You can assign the whole argument array, though:
my @date_string_array = @_;
for my $date_string (@date_string_array) {
...
Upvotes: 3
Reputation: 1818
You need to pass the refenrece to array.
my @date_strings_array = ("Jun 1, 2026", "Aug 26, 2018 GMT-05:00", "Aug 26, 2018");
print_datetimes(\@date_strings_array);
sub print_datetimes {
my $date_string_array = shift;
foreach my $date_string (@$date_string_array) {
print "The current iteration is $date_string.\n";
}
}
Upvotes: 1