You're not really explaining what you want to do. Because, as far as I can tell, your first script works just fine. Once you get the $hr, you print Dumper($hr), and the output you see to your screen will look like:

$VAR1 = { 'class_name' => '1st level Fighter/1st level Thief/1st level + Mage', 'intelligence' => '18', 'wisdom' => '14', 'charisma' => '15', 'experience' => '0', 'dexterity' => '18', 'strength' => '18(93)', 'last_name' => 'Zendelic', 'generic_class_name' => 'Fighter-Thief-Mage', 'filename' => '!ZeKy', 'generic_race' => 'Half-Elf', 'constitution' => '17', 'race' => 'Half-Elf', 'alignment' => 'Neutral Good', 'gender' => 'Female', 'id_name' => 'Kymaria_Zendelic', 'first_name' => 'Kymaria' };
That means that $hr is a reference to a hash (see the braces? Just like in your perl code when you want a reference to an anonymous hash). You could print ref($hr) to see that it says HASH. That means that if you put the rest of your character-handling-code inside that loop, and use $hr as a reference to a hash containing all the variables you care about, you should be off and running:
while (defined (my $hr = $csv->getline_hr($io))) { # use $hr->{id_name} to get the id_name }
You can't use Tie::IxHash because, well, you're not populating the hash. You'll probably have to extract things yourself in the order you want, but that's ok, you already have the order you want as @names. (Using DBD::CSV allows you to do this, too, but we'll stick with what you have for now.)

So, you'll have this loop:

while (defined (my $hr = $csv->getline_hr($io))) { my %general_information = ( "class(es)" => $hr->{class_name}, alignment => $hr->{alignment}, race => $hr->{race}, gender => $hr->{gender}, ); my %ability_scores = ( strength => $hr->{strength}, dexterity => $hr->{dexterity}, constitution => $hr->{constitution}, intelligence => $hr->{intelligence}, wisdom => $hr->{wisdom}, charisma => $hr->{charisma}, ); # do stuff with the above hashes. }
Mind you, that does seem somewhat repetitive, and I'm not a fan of repetition, so ...
while (defined (my $hr = $csv->getline_hr($io))) { my %general_information = ( "class(es)" => $hr->{class_name}, map { $_ => $hr->{$_} } qw/alignment ra +ce gender/; ); my %ability_scores = ( map { $_ => $hr->{$_} } qw/strength dexterity constitution intelligence wisdom charisma/ ); # here you can use them, same as previous example. list_loop(\%general_information); list_loop(\%ability_scores); }
Now, in these cases, you can use Tie::IxHash if you want to control the order of the keys, otherwise the order will be different each time you run (possibly).


In reply to Re^3: How to set variable names using Text::CSV? by Tanktalus
in thread How to set variable names using Text::CSV? by Lady_Aleena

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.