I know that I have sought your advice on this before, but I'm still kinda stuck with this problem. I have a program which takes a structured format file and reports back data contained in specified fields. I am writing a program which will "join" the output of 2such files. I have gathered from all of you that my best bet would be to use a relational database. So, armed with DBI, DBD::CSV, etc and my new "Programming the Perl DBI" book, I am off to tackle this once again. First, let me demonstrate what I am trying to accomplish. eg: exfields.pl file1.gz name,ipAdd,DestDev which produces the following output:
"atlanta" "192.168.1.1" "vienna" "miami" "192.168.1.2" "dallas" "nyc4" "192.168.1.3" "boston"
As you can see, "name,ipAdd,DestDev" are names of structured fields within the gzipped file. Let's say I ran this:
exfields.pl file2.gz name,ifSpeed
which produces the following output:
"atlanta" "622000000" "miami" "155000000" "nyc4" "155000000"
my new program would "merge" the output of both of those files producing:
"atlanta" "192.168.1.1" "vienna" "622000000" "miami" "192.168.1.2" "dallas" "155000000" "nyc4" "192.168.1.3" "boston" "155000000"
So, here's what I've got so far:
#!/usr/local/bin/perl -w ## merge.pl use strict; if ($ARGV[3] ne "") { my $file1 = $ARGV[0]; my $list1 = join (",", $ARGV[1]); my $file2 = $ARGV[2]; my $list2 = join (",", $ARGV[3]); my %hash_1 = process($file1, $list1); my %hash_2 = process($file2, $list2); }else { print "merge.pl - Usage: file field(s) file field(s)\n"; } sub process { my ($file, $list) = @_; my $script = "perl /home/limo/Perl/exfields.pl -e"; my %hash; my $arg; for $arg (split /,/ => $list) { open(FILE, "$script $arg $file |") or die "System error: $!\n"; while (<FILE>) { next if /^(#|none|unkno)/i; chomp; push @{ $hash{$arg} }, split; } close FILE; } return %hash; }
I suppose that I have three questions: 1. It was sugessted that I return a reference to a hash, thus conserving some memory. Would that just be, return /%hash? 2. In any case, what kind of print statement can I insert to test the contents of each hash, as returned? 3. I can't seem to find any information regarding reading data contained in a hash into DBI? Oh yeah, the reason that I am using a hash is that I would like the keys of the hash to become the column headers in the database table that I create. Any direction/help, as always, is much appreciated!

In reply to Data in Hash - DBI by Limo

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.