
FishMonger
Veteran
/ Moderator
Mar 4, 2014, 8:54 AM
Post #4 of 5
(4048 views)
|
Re: [technoman007] how to split a table
[In reply to]
|
Can't Post
|
|
Please use the code tags when posting code. I added them for you in this case, but please remember to use them in the future. First, you need to work on using proper code indentation. Proper indentation makes the code easier to read, understand, and maintain. Second recommendation would be to use the DBI module when interacting with databases instead of using shell commands. The DBI module will give you greater flexibility and control over the db interaction and would allow you to retrieve the records and build the hash in 1 simple statement. http://search.cpan.org/~timb/DBI-1.631/DBI.pm In order to parse your current $result data, you need to use the split function to separate each line placing them into an array.
my @result_set = split /\n/, $result; Next, use either the shift or splice function to strip off the header and dashed separator line, leaving just the data. http://perldoc.perl.org/functions/shift.html http://perldoc.perl.org/functions/splice.html Then loop over the remaining array elements and split the line into its 3 fields/vars and use those 3 vars to assign that row of data to a HoH (Hash of Hashes) or AoH (Array of Hashes). The choice of data structure will depend on the name field. If there is a possibility of having duplicate names, then you'll want to use the AoH, otherwise I'd use a HoH. I'm describing what needs to be done rather than giving the complete code for 2 reasons. 1) You've posted this in the Intermediate area which make us assume you have some Perl experience. 2) I get the feeling that this is a class homework assignment which you need to work out.
|