Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^4: How to set variable names using Text::CSV?

by Lady_Aleena (Priest)
on Oct 16, 2009 at 06:16 UTC ( [id://801503]=note: print w/replies, xml ) Need Help??


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

Tanktalus,

If I knew you a bit better, I would kiss you all over the face. The best I can do is give you some zen-cookies. You answered the big question I didn't know to ask, what was the proper hashref. This is what happens when one leaves a script for a long while and comes back. Without semantic names, one forgets what is what. So, I was left with a whole bunch of letters that were meaningless and not knowing what was doing what, because I am still not good at this. Use this as a cautionary tale about nonsemantic variable names.

I still couldn't get it to dump the big hash tied up, but that is okay. I don't need it.

Here is the script that works!

#!/usr/bin/perl use strict; use warnings; use diagnostics; use Text::CSV; use Tie::IxHash; tie my %general_information, qw(Tie::IxHash); tie my %ability_scores, qw(Tie::IxHash); open(my $io, "player_characters.csv") || die("can't open player_charac +ters.csv: $!"); my $csv = Text::CSV_XS->new({ sep_char => '|', allow_whitespace => 1, blank_is_undef => 1, }); $csv->column_names ($csv->getline ($io)); sub list_loop { my $hash_ref = shift; print "<ul>\n"; for my $key (keys(%$hash_ref)) { my $value = $hash_ref->{$key}; print "\t<li><strong>".ucfirst $key.":</strong> ".$value."</li>\n" +; } print "</ul>\n"; } while (defined (my $hr = $csv->getline_hr($io))) { %general_information = ( "class(es)" => $hr->{class_name}, map { $_ => $hr->{$_} } qw(alignment race gender) ); %ability_scores = ( map { $_ => $hr->{$_} } qw(strength dexterity constitution intelli +gence wisdom charisma) ); print qq{<h2><a href="$hr->{filename}" id="$hr->{id_name}">$hr->{fir +st_name} $hr->{last_name}</a></h2>\n}; list_loop(\%general_information); list_loop(\%ability_scores); }

While you are not a fan or repetition, I am not that big a fan of sprawl. I hope you don't mind.

Have a nice day!
Lady Aleena

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://801503]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-23 18:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found