10

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.

1
  • 2
    In JS, objects are basically the same thing as hashes/associative arrays. In Perl they're distinct ideas. If you just need a thing to put data in, you don't need an object. Objects are things with behavior. Commented Aug 9, 2011 at 20:55

3 Answers 3

18

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";
}   

And then you can add more elements to the array:

push @list, { width => 40, height => 70 };
Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

3

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.