Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
If it's useful it's useful! :-)

Perhaps you could consider loading you data into one data structure instead of three arrays.

You might also want a more general purpose approach where it would be easier to change the number and name of the columns.

Putting aside the question of commas inside the fields and any other error checking one approach might be like this.

It creates an array of hashes. If you were thinking of using something like HTML::Template for your output this would be very handy! :-)

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @AoH; my @fields = qw(subject grade rank); while (my $record = <DATA>){ chomp $record; my @values = $record =~ /([^,]+)/g; push @AoH, { map {$fields[$_] => $values[$_]} (0..$#fields) }; } print Dumper \@AoH; __DATA__ english,1,1 history,2,2 science,3,3 biology,4,4
output:
---------- Capture Output ---------- > "C:\Perl\bin\perl.exe" _new.pl $VAR1 = [ { 'subject' => 'english', 'grade' => '1', 'rank' => '1' }, { 'subject' => 'history', 'grade' => '2', 'rank' => '2' }, { 'subject' => 'science', 'grade' => '3', 'rank' => '3' }, { 'subject' => 'biology', 'grade' => '4', 'rank' => '4' } ]; > Terminated with exit code 0.

Hope that helps.


In reply to Re^2: Using Split to load a hash by wfsp
in thread Using Split to load a hash by Grey Fox

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-20 04:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found