Professional Web Applications Themes

CSV type data into hash? - PERL Beginners

Hey all, I seem to have a block on figuring this out. Hoping someone can help... I have a process that grabs a CSV export (from Excel server list). It looks something like this; Name,Location,Function,IPAddress uas123,123 street,Mail,10.11.12.13 uas321,123 street,Mail,10.11.12.14 What I want to do is load the whole thing into a data structure (I was thinking a hash). So I can do a Net View $hash{IPAddress}, and then print the results to an html doent that lists some of the fields in the CSV lines... Any suggestions on what type of data structure (I have been thinking of using a ...

  1. #1

    Default CSV type data into hash?

    Hey all, I seem to have a block on figuring this out. Hoping someone
    can help...

    I have a process that grabs a CSV export (from Excel server list).

    It looks something like this;
    Name,Location,Function,IPAddress
    uas123,123 street,Mail,10.11.12.13
    uas321,123 street,Mail,10.11.12.14

    What I want to do is load the whole thing into a data structure (I was
    thinking a hash). So I can do a Net View $hash{IPAddress}, and then
    print the results to an html doent that lists some of the fields in
    the CSV lines...

    Any suggestions on what type of data structure (I have been thinking
    of using a hash with the IPAddress as the Key, and the Val = a hash of
    the CSV line (with the Key being the first or header line, and the
    Vals being the data)?

    Then how to do it? Specifically, How do I create hashs like this from
    the data file above (getting all the keys and values from the CSV)?

    %hash = (
    Name => uas123
    Location => 123 street
    Function => Mail
    IPAddress => 10.11.12.13
    )

    Hmmm, I wonder if I just answered my own question by writing it down.

    Thanks for any input/suggestions you may have!

    --
    Tim Musson
    Flying with The Bat! eMail v2.12.00
    Cannot open file "include <std/disclaimer>;"

    Tim Guest

  2. #2

    Default Re: CSV type data into hash?

    * Tim Musson <net> [2004-09-21T09:25:23] 

    As is often the case, I suggest going to the CPAN and searching for CSV.
    You'll find lots of relevent modules.

    Of course, if you want to take care of this yourself, as an exercise,
    consider something long the lines of the following:

    open my $csv_file, '<', "filename.csv"
    or die "couldn't open file!";
    my columns = split(/,/, <$csv_file>);
    chomp $columns[-1];
    my rows;
    while (<$csv_file>) {
    chomp;
    push rows, {};
    $rows[-1]{columns} = split /,/;
    }

    So:
    Read the first line and break it up on commas to get column names.
    Chomp the newline from the last column name.
    Keep reading line, chomping their newlines.
    Add an empty hash to the end of the rows, then populate it, matching
    column names to field positions.

    That code is off-the-cuff, but should work, I think. :) If not, or if
    you want more information, just holler.

    --
    rjbs

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.0.7 (GNU/Linux)

    iD8DBQFBUC5H5IEwYcR13KMRAiUIAJwKme3A7ZqZMX+fD1iMiw AiT9A6LgCfesgy
    HoduonszsdHFSwCIAWy5ayo=
    =qb+V
    -----END PGP SIGNATURE-----

    Ricardo Guest

  3. #3

    Default Re: CSV type data into hash?

    On Tue, 21 Sep 2004, Ricardo SIGNES wrote:
     

    That's optimistic.

    id,name,title
    42,"Adams, Douglas","Author, HHGTTG et al"

    id;name;title
    42;Adams, Douglas;Author, HHGTTG et al

    Etc. There are various ways -- most ad hoc -- of embedding commas in CSV
    files, nearly all of which will fail if you just split on commas.

    One good way around this is to use DBD::CSV, and just write normal DBI
    code that treats your CSV file as if it were a SQL database. This works
    surprisingly well, and is much easier to do if you already know DBI.

    Splitting on commas, as above, is the "easy" way to do it, but it should
    really only be done if you *know* your data file is well behaved, i.e.
    you wrote it, you've validated it, it has no funny rows, etc. If there's
    any reasonable chance that the data gets "funny", things get much harder
    to do right and you're better off going with a module like DBD::CSV.


    --
    Chris Devers
    Chris Guest

Similar Threads

  1. Converting from data type varchar to data type money
    By schaudry in forum Coldfusion - Advanced Techniques
    Replies: 6
    Last Post: June 10th, 11:01 PM
  2. Converting data type varchar to data type money
    By schaudry in forum Coldfusion - Getting Started
    Replies: 1
    Last Post: June 10th, 07:22 PM
  3. Data::Dumper->dump()ing a hash?
    By Dan Anderson in forum Perl / CGI
    Replies: 4
    Last Post: November 9th, 08:12 PM
  4. Replies: 3
    Last Post: October 18th, 06:56 AM
  5. Replies: 2
    Last Post: October 16th, 08:29 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139