Hello Lennotoecom

Good effort, but a couple of things, most importantly, using the mode in the second argument of 2 argument open is right out.
Use the 3 arg open

open IN, '<', 'filepath' or die '$!';

While there is much temptation to predict knowledge of data not provided, don't.

Within file1 all of the tab delimited headers are required. There is no 'crap' in file1. This means that the hash read in from file1 is the combination of keys needed for the slice in file2.

So you do not need to re-write input from file1. Also your map in file2 read, being used to extract the keys for the slice, is uneccessary. Well the part which increments the numbers for the keys. The required column headers already exist as the hash keys read in from file1. As for actually filing the data into a hash, (the rest of the map) you may be on the right lines.

your split appears malformed, final /? may also be unneccessary as split operator defaults on whitespace including newline, and also defaults the special variable $_

so to read in the cols for use against file2 something like

#!/usr/bin/perl use warnings; use strict; my $wantcols, '<', '/path/to/file1' or die '$!'; my @cols; while(<$wantcols>){ push @cols, split; } close $wantcols;

Then to extract through file2, just read through and load the rows

my @valarray = [ @cols ]; #construct wanted headers my $datavalues, '<', '/path/to/file2' or die '$!'; # push rows of wanted columns onto table while(<$datavalues>){ push @valarray, [ (split)[@cols] ]; } close $datavalues;

using split (acting on default) within an array constructer allows us to treat the split lines as an array slice so we can just load the rows as array references within the valarray. This also assumes split operates on the special variable at this level of misanthropy.

hmm, ok, surprised myself here. however important to note, i have made the assumption that the column headers are numbers, but this is mentioned in op post. And somehow relieved the requirement for using hashes at all. Which is fine until a specific data element needs fetching. But for this you can just construct a hash or call an array element as needed.

print the table of extracted columns you now need to print out the array of referenced arrays.

print map { @{ $valarray[$_] } , $/ } 0..$#valarray;

by the stars, i hope that compiles! the important lessons here though are do not make up data, and definetely do not proceed with opens where the arguments are on no account less than 3. (unless your long robe is white, with a fair weight of gold trim exquisitely sewn by the handmaidens of ash'kabha)

edit - s/&lt;/</; re monk advised (tx)


In reply to Re^2: Extracting Columns from line by Don Coyote
in thread Extracting Columns from line by snape

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.