Limo has asked for the wisdom of the Perl Monks concerning the following question:

How do I:
@ARGV = ( $file_1 $a,$b | $file_2 $c,$d )
The single letter variables are specific header labels. I just want to open each file and:
%first_file = (a => data in this column, b => data in this column) %second_file = (c => data in this column, d => data in this column)

Replies are listed 'Best First'.
Re: @ARGV Manipulation
by merlyn (Sage) on Sep 15, 2000 at 20:34 UTC
    Are you asking how to set @ARGV that way? Or are you saying it already contains something like:
    @ARGV = qw(file1 a b file2 c d);
    And if so, how do we know when the filenames end and the key letters begin? And what kind of data structure do you want this in? And what defines a column?

    -- Randal L. Schwartz, Perl hacker

      Ok, one more time. Here is a very direct question that would really help me get started. How do I parse the elements of:
      merge.pl file_1 a,b,c file_2 d,e,f
      so that:
      file_1 a,b,c
      gets placed in a hash where:
      keys = "a", "b", "c"
      and:
      key values = /$aref_a, /$aref_b, /$aref_c
      and: file_2 d, e, f </CODE> gets placed in another hash where:
      keys = "d", "e", "f"
      and:
      key values = /$aref_d, /$aref_e, /$aref_f
      I believe that I can figure out the rest. This has just been a sticking point for me. Obviously. And regarding your "hire a programmer" comment. Look. I am NOT a programmer. I am a network engineer, tasked with writing some tools for my group. I have stated in just about every one of my posts that I am new to Perl. So, it shouldn't surprise anyone if I am having trouble expressing ideas in a programming context. Would you tell a 2 year old, who is just learning to speak, to "hire a tutor" before asking you a question? At any rate, I don't mean to sound ungrateful for everyone's help, nor do I want anyone to do my work for me. Just a little direction would be great.
        I'm lost about where you're lost. Here's the things that I think you'll need to know to do this task, but I'm not sure which of these you know and which you don't.
        • how to look at @ARGV
        • how to walk through an array with a foreach loop
        • how to assign things to a hash
        • what a hash can contain
        • the difference between \ and / as it applies to Perl
        • how to recognize the difference between file_1 and a,b,c on the command line
        • how to turn a,b,c into a 3-element list
        • how to populate an arrayref
        • how to read a file into an arrayref
        • how to recognize the things from the file to figure out where they'll go
        • other things that I didn't think of when I was coming up with this list
        I enjoy helping people. But I keep wondering "help you with WHAT?" on each post. Ask a more specific question, perhaps any of the above. If it's all of the above, then I suggest you need a good book or two, or spend some time with Perl tutorials.

        -- Randal L. Schwartz, Perl hacker

Re: @ARGV Manipulation
by Limo (Scribe) on Sep 16, 2000 at 00:01 UTC
    You know, I wish that I were a better programmer, so that I could be more succinct, but since Perl is very new to me, I do the best that I can, and try and learn from you folks. To answer your questions: @ARGV is set on the command line using the following syntax:
    merge.pl file_1 a,b,c file_2 d,e,f
    where "a,"b",c" and "d,"e",f" are column headers. Columns are defined in the files as such:
    #DFD ' ' #H Period Interface SrcRouter Subset MbitsCap Polls MbpsIn MbpsOut MbpsSum InMinUtil In5pUtil InAvgUtil In95pUtil InMaxUtil InUtilSD InMaxTS OutMinUtil Out5pUtil OutAvgUtil Out95pUtil OutMaxUtil OutUtilSD OutMaxTS EffMinUtil Eff5pUtil EffAvgUtil Eff95pUtil EffMaxUtil EffUtilSD EffMaxTS Class InPctDiscards InPctErrors InPctDiscErr InPctDiscErr95Q OutPctDiscards OutPctErrors OutPctDiscErr OutPctDiscErr95Q SecSysUpTime InWrapEvts InWrapCnts OutWrapEvts OutWrapCnts PctPollsGT80pUtil PctPollsGT1pDiscErr Flags KppsIn KppsOut IfDescr IfIndex #F %-17s %-35s %-15s %-6s %9.5f %7d %14.3f %14.3f %14.3f %7.3f %7.3f %7.3f %7.3f %7.3f %9.5f %14s %7.3f %7.3f %7.3f %7.3f %7.3f %9.5f %14s %7.3f %7.3f %7.3f %7.3f %7.3f %9.5f %14s %3s %7.3f %7.3f %7.3f %7.3f %7.3f %7.3f %7.3f %7.3f %10d %7d %7d %7d %7d %7.3f %7.3f %-10s %14.3f %14.3f %-30s %-10s
    Specifically, what I want to do is: From file_1:
    create a hash whose keys are any of the headers specified on the command line. Each key value is a reference to an array containing the data from each column.
    From file_2:
    create another hash whose keys are any of the headers specified on the command line. Each key value is a reference to an array containing the data from each column.
    NOTE: There will ALWAYS be matching data between both files, but NOT necessarily under the same header label! For example:
    the column "SrcRtr" from file_1 contains the exact same data as the column "DstDev" in file_2
    After assembling these data structures, I want to merge them, in structured format, and print the resulting merged file to <STDOUT>. I'm not sure if this is any clearer, but please let me know if it's not.