G'day redss,

While your example code has hard-coded values, I'll assume you're actually capturing this data from some source (user input, spreadsheet extract, etc.) and have placed the vales in $name, $addr and $city.

Create a module for your class. 'my_type' is a poor choice of name: all lowercase names are typically used for pragmata and it's totally uninformative; for want of something better, I've used 'Client::Data' below.

Take a look at "Perl OO Tutorial for Beginners". It's entry level and you may have read it previously; however, it had a major revision in 2013 (possibly more recent than your last read) and provides links to more detailed and advanced information.

The six lines of code you posted can now be reduced to:

push @client_list, Client::Data::->new(name => $name, address => $addr, city => $city +);

Note that I've used an array (no dereferencing of an arrayref required) with a more informative name. A hash (perhaps %client_data_for) may be a better choice — see ++GotToBTru's comments above.

I don't know whether "access the values later" means later in the code or at a later time in other code. If the latter, ++Laurent_R has addressed this above.

Accessing the data is easy. It will, of course, depend on whether you've used an array or a hash. I've no idea how you intend to use this, but let's say you want to find Mary Smith's city. [Note: I've assumed Moose/Mouse/Moo style accessors and the example code is untested.]

my $wanted_client = 'Mary Smith';

Array:

for my $client (@client_data) { if ($client->name eq $wanted_client) { print "${wanted_client}'s city is ", $client->city; last; } }

Hash:

print "${wanted_client}'s city is ", $client_data_for{$wanted_client}- +>city;

From this, it should be obvious why a hash lookup is faster. Of course, a hash may be inappropriate for your needs.

-- Ken


In reply to Re: storing records of data structure in perl? by kcott
in thread storing records of data structure in perl? by redss

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.