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

Hi, i am trying to write a sub-routine to split an input file into columns which can be accessed by calling that particular element of the array. e.g the input file contains 10 columns of data separated by tabs, therefore i am using split to separate every time it encounters one or more consecutive white spaces, anyway my sub-routine appears to be splitting by row instead of column. can anyone see whats wrong??
#! /usr/local/bin/perl -w use strict; open (FILE, $ARGV[0]) or die "unable to open file"; open (OUTFILE, ">$ARGV[1]"); my @array = <BLASTX>; my $line; split_file (@array); sub split_file { # my $line = $_; chomp ($line); # my @array = @_; my (@array) = split(/\s+/, $_); $array[8] =~ s/\%//g; return @array; }
n.b this is just a snippet of code. also i want to print say $array2 etc

Replies are listed 'Best First'.
Re: split function
by Zaxo (Archbishop) on Jun 18, 2002 at 10:28 UTC

    You can pull the file into a 2-d array more easily:

    #!/usr/bin/perl -w use strict; open (FILE, $ARGV[0]) or die $!; my @array = map {[split " "]} <FILE>; close FILE or die $!; for (0..$#array) { $array[$_][8] =~ s/%//g; }
    The quoted space arg to split is magical, it takes care of leading and trailing whitespace too.

    After Compline,
    Zaxo

Re: split function
by Ido (Hermit) on Jun 18, 2002 at 10:29 UTC
    First of all, slurping a whole file into an array is usually a very bad idea. instead use
    while(<FILE>){ #line is in $_ }
    Now, if you want to split on a tab, split on a tab. And..something is very wired over there, you split $_ but chomp $line (and don't assign $_ to $line...) You want something like:
    while(<FILE>){ chomp; my @columns=split /\t/; $columns[0]=~tr/%//d; # When you want to get rid of single characters +tr/// should be better than s/// #do what ever you want with @columns.. }
Re: split function
by zejames (Hermit) on Jun 18, 2002 at 10:47 UTC
    Hello, I don't really understand your code :
    • You open FILE and you use BLASTX
    • You never use the array passed to the sub split_file
    • you return an array, that you never use

    I would do this :
    open FILE, "< $ARGV[0]" or die "Unable to open file : $!\n" while (<FILE>) { push @{$data[$.]}, split /[\s\t]+/; }
    (No tested)
    $. is the line number of the input file. Hence, if you have a test file like this
    1 2 3 4 5 6 7 8 9 A B C
    $data[ 2 ] [ 1 ] will give '6'

    HTH

    -- zejames