Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl - Array of Objects

Tags:

perl

Noob question here.

I'm sure the answer will be create objects, and store them in an array, but I want to see if there is an easier way.

In JSON notation I can create an array of objects like so:

[
  { width : 100, height : 50 },
  { width : 90, height : 30 },
  { width : 30, height : 10 }
]

Nice and simple. No arguing that.

I know Perl is not JS, but is there an easier way to duplicate an array of objects, then to create a new "class", new the objects, and push them in an array?

I guess what would make this possible is an object literal type notation that JS provides.

Or, is there another way to store two values, like above? I guess I could just have two arrays, each with scalar values, but that seems ugly...but much easier than creating a separate class, and all that crap. If I were writing Java or something, then no problem, but I don't want to be bothered with all that when I'm just writing a small script.

like image 647
Steve Avatar asked Aug 09 '11 19:08

Steve


3 Answers

Here's a start. Each element of the @list array is a reference to a hash with keys "width" and "height".

#!/usr/bin/perl

use strict;
use warnings;

my @list = (
    { width => 100, height => 50 },
    { width => 90, height => 30 },
    { width => 30, height => 10 }
);

foreach my $elem (@list) {
    print "width=$elem->{width}, height=$elem->{height}\n";
}
like image 78
Keith Thompson Avatar answered Sep 28 '22 19:09

Keith Thompson


an Array of hashes would do it, something like this

my @file_attachments = (
   {file => 'test1.zip',  price  => '10.00',  desc  => 'the 1st test'},
   {file => 'test2.zip',  price  => '12.00',  desc  => 'the 2nd test'},
   {file => 'test3.zip',  price  => '13.00',  desc  => 'the 3rd test'},
   {file => 'test4.zip',  price  => '14.00',  desc  => 'the 4th test'}
   );

then access it like this

$file_attachments[0]{'file'}

for more information check out this link http://htmlfixit.com/cgi-tutes/tutorial_Perl_Primer_013_Advanced_data_constructs_An_array_of_hashes.php

like image 45
Bob The Janitor Avatar answered Sep 28 '22 18:09

Bob The Janitor


Pretty much the same way you do it in JSON, in fact, use the JSON and Data::Dumper modules to produce output from your JSON that you could use in your Perl code:

use strict;
use warnings;
use JSON;
use Data::Dumper;
# correct key to "key"
my $json = <<'EOJSON';
[
  { "width" : 100, "height" : 50 },
  { "width" : 90, "height" : 30 },
  { "width" : 30, "height" : 10 }
]
EOJSON

my $data = decode_json($json);
print Data::Dumper->Dump([$data], ['*data']);

which outputs

@data = (
          {
            'width' => 100,
            'height' => 50
          },
          {
            'width' => 90,
            'height' => 30
          },
          {
            'width' => 30,
            'height' => 10
          }
        );

and all that is missing is the my

like image 24
MkV Avatar answered Sep 28 '22 18:09

MkV