[Thousand-Oaks-pm] Fwd: RE: Thousand Oaks PM cancelled this month

Chuck Hardin chardin at valueclick.com
Thu Dec 15 16:43:52 PST 2011


Forwarded because I don't believe it went to the list.

-------- Original Message --------
Subject: 	RE: [Thousand-Oaks-pm] Thousand Oaks PM cancelled this month
Date: 	Thu, 15 Dec 2011 15:05:54 -0800
From: 	Barry Brevik <BBrevik at StellarMicro.com>
To: 	Chuck Hardin <chardin at valueclick.com>, ThousandOaks.pm 
<thousand-oaks-pm at pm.org>



Of course I failed to notice this email, so like an idiot, I showed up
at ValueClick last night<g>.

Since I missed out, I thought I would share this very short snippet with
you.

I have to deal with many Excel files and CSV files, so I have code that
reads each line into an array to be processed. The problem is that my
users keep jacking around the column order before giving me the final
file. I decided to fix that by reading row 0 (or, to my great annoyance,
row 1 in Excel-speak), which in my case always contains column headers,
look for the column names I need and extract the array offset to use in
extracting data from subsequent rows.

There is, of course, more than 1 way to do this, but to keep it simple,
this is the way I do it. One gotcha is if your array contains any
duplicate values, you are screwed. My apologies if this code is too
elementary for the list members:

use warnings;
use strict;

# Array represents columns read from a CSV file.
# Or whatever you want.
my @array = ('zero', 'one', 'two', 'three', 'four');

# Create hash where the keys are the values from the
# array, and the values are the positions in the array.
my $i = 0;
my %hash = map {$array[$_] =>  $i++} (0..$#array);

# Display the results.
print "$_, $hash{$_}\n" foreach (keys(%hash));



-- 
Chuck Hardin 	Software Engineer, ValueClick Media
+1-818-575-4573 	chardin at valueclick.com <mailto:chardin at valueclick.com>





This email and any files included with it may contain privileged,
proprietary and/or confidential information that is for the sole use
of the intended recipient(s).  Any disclosure, copying, distribution,
posting, or use of the information contained in or attached to this
email is prohibited unless permitted by the sender.  If you have
received this email in error, please immediately notify the sender
via return email, telephone, or fax and destroy this original transmission
and its included files without reading or saving it in any manner.
Thank you.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/thousand-oaks-pm/attachments/20111215/0f8ac95c/attachment.html>


More information about the Thousand-Oaks-pm mailing list