Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Text::CSV will store your data as an array of hashes with each hash key being the column name. This seems to make the most sense. For example:</p> <pre><code>my %employee = %{ $employee_array[2] }; #Row #3 of your file: print "The name of the third employee is $employee{first_name} $employee{last_name}\n"; </code></pre> <p>Thus, a single row of your array contains all of the data for that employee.</p> <p>In your case, you'd have to keep the index the same across multiple arrays:</p> <pre><code>print "The name of the third employee is $first_name[2] $last_name[2]\n"; </code></pre> <p>If you had a function that operated on the employee, you'd have to pass all of your arrays to the function:</p> <pre><code>print_paycheck($first_name[1], $last_name[1], $employee_num[1], $hire_date[1]...); </code></pre> <p>While if you had an array of hashes, you could do this:</p> <pre><code>print_paycheck($employee_array[1]); </code></pre> <p>I take it that you don't know about references. Many beginner Perl books don't discuss them, and they're not an obvious extension of Perl. However, references allow you to make these more complex data structures. Fortunately, Perldoc has an excellent <a href="http://perldoc.perl.org/perlreftut.html" rel="nofollow">Tutorial</a>. I suggest that you read it. </p> <p>In reality, you probably want to to store your data keyed by the employee number, so you want a hash of hashes.</p> <p>Here's an example of a hash of hashes. <strong>NOTE</strong>: This is <em>not</em> the way I'd do the program. First, I would use <code>Text::CSV</code> if available, and then I would actually use an object oriented approach. However, I wanted to keep this as a simple <em>hash</em> of <em>hashes</em>:</p> <pre><code>use warnings; use strict; use feature qw(say); use Data::Dumper; my %employee_hash; &lt;DATA&gt;; #Field Names while ( my $employee_data = &lt;DATA&gt; ) { chomp $employee_data; my ($employee, $birth_date, $first_name, $last_name, $gender, $hire_date) = split /,/, $employee_data; $employee_hash{$employee}-&gt;{birth_date} = $birth_date; $employee_hash{$employee}-&gt;{first_name} = $first_name; $employee_hash{$employee}-&gt;{last_name} = $last_name; $employee_hash{$employee}-&gt;{gender} = $gender; $employee_hash{$employee}-&gt;{hire_date} = $hire_date; } for my $employee ( sort keys %employee_hash ) { my $gender; if ( $employee_hash{$employee}-&gt;{gender} eq "M" ) { $gender = "he"; } else { $gender = "she"; } printf qq(Employee: %s is %s %s and %s was hired on %s\n), $employee, $employee_hash{$employee}-&gt;{first_name}, $employee_hash{$employee}-&gt;{last_name}, $gender, $employee_hash{$employee}-&gt;{hire_date}; } __DATA__ emp_no,birth_date,first_name,last_name,gender,hire_date 10001,1953-09-02,Georgi,Facello,M,1986-06-26 10002,1964-06-02,Bezalel,Simmel,F,1985-11-21 10003,1959-12-03,Parto,Bamford,M,1986-08-28 10004,1954-05-01,Chirstian,Koblick,M,1986-12-01 10005,1955-01-21,Kyoichi,Maliniak,M,1989-09-12 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload