Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

storing records of data structure in perl?

by redss (Monk)
on May 20, 2015 at 20:23 UTC ( [id://1127278]=perlquestion: print w/replies, xml ) Need Help??

redss has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks! I want to create a database-type data structure and store records in a stack then access the values later. I guess the best way would be an array of objects or an array of hashes? Here's what I have so far
my $obj = bless( { 'name' => 'joe blow', 'address' => '1 main street', 'city' => 'Miami' }, 'my_type' ), push( @$var, $obj );
My question is, is this the easiest way to do it, and how would I later access the values for each record?

Replies are listed 'Best First'.
Re: storing records of data structure in perl?
by Laurent_R (Canon) on May 20, 2015 at 20:33 UTC
    Your requirement is not entirely clear to me, but you may want to look at the Storable module. In some simple cases, the Data::Dumper also makes it possible to create persistent data on disk.

    For more complicated cases, you may also want to take a look at CPAN modules dealing with JSON or Yaml data formats.

    But maybe I did not understand well your requirement and you don't need data persistence. If you were only asking about which in-memory data structure to use, then you might have to give a bit more details.

Re: storing records of data structure in perl?
by GotToBTru (Prior) on May 20, 2015 at 20:36 UTC

    There are modules to help you create and use databases. Here, see Database Programming.

    Update: consider how you will retrieve your records when you need them. You may want to store your database as a Hash of Hashes (HoH). Retrieval will be very fast. See Perl Datastructures.

    Dum Spiro Spero
Re: storing records of data structure in perl?
by kcott (Archbishop) on May 21, 2015 at 02:36 UTC

    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

Re: storing records of data structure in perl?
by boftx (Deacon) on May 21, 2015 at 09:37 UTC

    One word: DBD::SQLite

    Okay, this presumes you know about DBI and SQL, but hey, you know about the monestary so you aren't clueless. :)

    You must always remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.
Re: storing records of data structure in perl?
by jeffenstein (Hermit) on May 21, 2015 at 10:00 UTC
    Another option is DBM::Deep, which lets you store nested structures. However, if the structures are actually objects (blessed references), I don't believe it will work, as DBM::Deep re-blesses the references.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1127278]
Approved by Laurent_R
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (1)
As of 2024-04-25 01:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found