I have a tab delimited file that is a DB export of an addressbook. I want to map the data from this file into a hash that has associative keys. I can read the data into an array with no problem, but is there an easy way to map this into the hash? Or is there a way to split things into the hash instead of an array? I believe going from the array to a hash isn't too difficult, but I need to get to an associative hash. Maybe array -> hash -> associative hash? Don't know, that's why I'm posting this!

/usr/bin/perl -w use strict; my $import_file = shift || 'addresses'; my @tmp; my %import_data =( 'id' => '', 'username' => '', 'domain' => '', 'address' => '', 'firstname' => '', 'lastname' => '', 'nickname' => '', 'type' => '', 'street' => '', 'city' => '', 'state' => '', 'zip' => '', 'country' => '', 'company' => '', 'home_phone' => '', 'work_phone' => '', 'pager' => '', 'cellular' => '', 'fax' => '', 'other' => '', 'birth_month' => '', 'birth_day' => '', 'birth_year' => '', 'birthday' => 'undef', 'title' => '', 'status' => '', 'time_stamp' => '', 'middleinitial' => '', 'nameprefix' => 'undef', 'namesuffix' => 'undef', 'street2' => '', 'website' => '', 'notes' => '', 'department' => 'undef', 'assistant' => 'undef', 'dstreet' => '', 'dstreet2' => '', 'dcity' => '', 'dstate' => '', 'dzip' => '', 'dcountry' => '', 'dphone1' => 'undef', 'dphone2' => 'undef', 'daddress' => '', 'dspouse' => 'undef', 'danniversary_day' => '', 'danniversary_month' => '', 'dnotes' => 'undef', 'dwebsite' => '', 'daddress2' => '', 'danniversary_year' => '', 'category' => '', 'prime_phone' => '', ); open(FILE, "< $import_file") or die "Cannot open $import_file ERR: $!\ +n"; while (<FILE>) { @tmp = split /\t/; #map @tmp to %import_data here #then call a sub-routine to manipulate } close FILE;

Thank You,

-THRAK
www.polarlava.com

janitored by ybiC: <readmore> tags around long codeblock, as per Monastery convention


In reply to Mapping to an associative hash by THRAK

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.