Last active
June 7, 2019 15:57
-
-
Save sonjz/051ebfaeeca23400bd62dea6ad21886a to your computer and use it in GitHub Desktop.
dealing with arrayref of hashes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# tested on https://www.tutorialspoint.com/execute_perl_online.php | |
use warnings; | |
use strict; | |
use Data::Dumper; | |
my @errors = ( | |
{ | |
at => "Thu Jun 6 20:31:11 2019", | |
error => "/url/1/2/3: 403 Forbidden" | |
}, | |
{ | |
at => "Thu Jun 7 20:31:11 2019", | |
error => "/url/1/2/3: 403 Forbidden" | |
} | |
); | |
# when i receive it, it is $errors, passed in as arrayref of hashes | |
my $errors = \@errors; | |
my $errorOutput = ""; | |
# be sure to cast as array again for foreach loop | |
foreach my $error (@$errors) { | |
#print Dumper($error); | |
$errorOutput .= @$error{'at'} . ": " . @$error{'error'} . "\n"; | |
} | |
# i want to split by ": " and join as indented new-line | |
$errorOutput = join("\n ", split(": ", $errorOutput)); | |
print $errorOutput; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment