Skip to content

Instantly share code, notes, and snippets.

@sonjz
Last active June 7, 2019 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sonjz/051ebfaeeca23400bd62dea6ad21886a to your computer and use it in GitHub Desktop.
Save sonjz/051ebfaeeca23400bd62dea6ad21886a to your computer and use it in GitHub Desktop.
dealing with arrayref of hashes
# 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