in reply to extracting single column

#!/bin/perl -w use strict; #for a csv file you can try something like this, #but there are dozens of ways to do this. #you should read a book called "Sam's learn perl in #24 hours" (or similar title). #variables you can change to suit your needs my $column_separator = ","; my $column_number = "3"; $column_number--; my $file="filename.ext"; open(FILE,"<","$file"); my @lines=<FILE>; close FILE; foreach my $line (@lines){ my @columns = split(/$column_separator/,"$line"); print $columns[$column_number],"\n"; }

Replies are listed 'Best First'.
Re^2: extracting single column
by Sewi (Friar) on Feb 10, 2012 at 09:47 UTC
    There are few lines where I don't agree:

    my $column_separator = ",";
    Better:
    my $column_separator = qr/,/;
    ...imaging someone wants to use "." as separator.

    my $column_number = "3";
    Don't write numbers as text, write them as numbers (cut away those " chars).

    Loading the whole file into memory might not be that good idea, imagine it's a 10 GB file. Why not parse it line-by-line?

    open(FILE,"<","$file"); while (<FILE>) { my @columns = split(/$column_separator/); print $columns[$column_number],"\n"; } close FILE;

Re^2: extracting single column
by Anonymous Monk on Jun 04, 2013 at 04:19 UTC
    Hey my column has around 900 entries , this code does not extract all of them , but only a part of them . so if you can modify this for me , I will be grateful