I don't get it.

To "insert drives for a unit", do you insert/update a row in the drives table or insert/update the number of drives in the table for the unit? If it's the latter, I don't see why there's a problem.

Do you have one table for units, and another for drives? I assume you do, because make/model is important to you. Well, assuming you want info on every drive in every system, relational database design says you should have one table for the systems, and another table for the drives. You give each system a unique identifier, and make sure that each entry for a drive contains a field that tells you what system the drive sits in (by containing that system's identifier).

Why are you worried about control structures? You don't want to hard-code cases for different numbers of drives? Perl can easily loop over all the members of an array. So, if you store the information for the drives in an array of hashes (see perldoc perlreftut and read up on references for more info), the task is pretty simple.

anyhow, assuming that setup, and that you have a table for systems, and anotehr table for drives in your MySQL database, it's not that hard to code. For each drive, get all the info and push a reference to that info, as a reference to a hash, into an array.

# I assume @systems is already initialized and full of data. # the important thing is that you do this loop for each # system. my @drives; #loop over the systems foreach (@systems) { while ( THERE ARE MORE DRIVES ) { #get drive info push @drives, {system=>SYSTEMID, make=>MAKE, model=>MODEL, capacity=>CAPACITY, status=>STATUS }; }

Then, when inserting the drives (the logic's similar for updating) you can just do this:

# prepare once, execute often my $sth= $db->prepare("INSERT INTO drives (system_id, make, model, cap +acity, status, last_update) VALUES (?,?,?,?,?,NOW())"); foreach my $drive (@drives) { $sth->execute($drive->{system}, $drive->{make}, $drive->{model}, $ +drive->{capacity}, $drive->{status}); }

That way, you don't have to hard-code any numbers. But obviously this assumes a lot of background setup. Your drives table should have an auto_increment ID field, the last_update should be a DATETIME or a TIMESTAMP datatype, and so forth.

I hope this gives you something to work with!

note updated to improve grammar and be more explicit (OK, verbose =)

perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'

In reply to Re: How many drives does it have? by arturo
in thread How many drives does it have? by TeKk9

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.