in reply to Re: Extracting Columns from line
in thread Extracting Columns from line

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)

Replies are listed 'Best First'.
Re^3: Extracting Columns from line
by Anonymous Monk on Oct 23, 2013 at 20:24 UTC
    most importantly, using the mode in the second argument of 2 argument open is right out.
    Why? Does the code run faster with the 3 argument open than tha 2 argument open?

      The reason is mainly security, redirection coupled with pathnames can produce nasty effects at unexpected filenames. Using the 3 argument open to separate the mode from the path can prevent regrets.

      I am currently unaware of performance stats