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

Ok, I'm confused with this one, namely because it used to work for a start! Don't have the faintest idea what has gone wrong now.
I have a text file ... looks similar to this other than it actually has 13 columns
.55 0.68 0.39 0.69 0.42 0.43 0.52 0.61 0.13 0.1 0.09 0.06 0.08 0.12 0.07 0.08 0.04 0.01 0.13 0.04 0.1 0.07 0.03 0.02 0.08 0.03 0.11 0.03 0.08 0.05 0.07 0.02
Out of those 13 columns I want to chose any 10 of them and then print them. So, one file might not have columns 1 3 and 6 . Another might not have 4, 7 and 9. I want to create files for all combinations of 10 columns .. making 120 files in total.
I first write a perl program that will give a .txt file like this
1 10 2 1 10 3 1 10 4 1 10 5 1 10 6 1 10 7 1 10 8 etc
I then feed this text file into the following perl script
#! /usr/bin/perl $combofile = $ARGV[0]; $spectralfile = $ARGV[1]; $diroutput = $ARGV[2]; $without = "spectrawithout"; $txt = ".txt"; open(COMBO, "$combofile") || die "Error: Can't open $combofile: $!\n"; while(<COMBO>) { @combo = split; $var1 = $combo[0]; $var2 = $combo[1]; $var3 = $combo[2]; $output = "$without$var1$var2$var3$txt"; system("chosespectraldata.pl $spectralfile $var1 $var2 $var3 > $di +routput$output"); }
Which then calls this one.
#! /usr/bin/perl $spectralfile = $ARGV[0]; $var1 = $ARGV[1]; $var2 = $ARGV[2]; $var3 = $ARGV[3]; $count = 0; open(SPEC, "$spectralfile") || die "ERROR: Can't open $spectralfile FI +LE for reading: $!\n"; foreach my $line (<SPEC>) { chomp $line; #@array = split(/\t/,$line); @array = split(/\,/,$line); for($i = 0; $i<@array; $i++) { $count = $i++; if(($count != $var1) || ($count !=$var2) || ($count !=$var3)) { print "$count $var1 $var2 $var3\n"; print "$array[$i],\t"; } } print "\n"; }

It doesnt work and I am straching my head as to why. I use the count variable because .. although it used to work for when column = 0, it doesnt now, at all. But the count variable doesnt increase at all, so it doesnt do what I think it should be doing. The files are being created ok but all 13 columns are being printed each time .. so I have 120 files that are exactly the same.
I just know this has to be a silly mistake but I can't find it. Any suggestions much appreciated.

Replies are listed 'Best First'.
Re: what on earth is going on?
by eric256 (Parson) on Nov 10, 2005 at 17:15 UTC

    It is hard to tell from your question. In the future instead of "doesn't work" and "variable doesn't increase" you should be more specific. What doesn't work, what variable isn't incrementing.

    That aside, line 19 of your script is spliting on , but you don't have commas you have tabs or spaces. BTW Answers will be better if you can provide actual working examples that we could test.


    ___________
    Eric Hodges $_='y==QAe=e?y==QG@>@?iy==QVq?f?=a@iG?=QQ=Q?9'; s/(.)/ord($1)-50/eigs;tr/6123457/- \/|\\\_\n/;print;
      Yes, and line 17 (splitting on tabs) has been commented...

      Flavio
      perl -ple'$_=reverse' <<<ti.xittelop@oivalf

      Don't fool yourself.
      Ok, well actually you hit the nail on the head. I am now splitting on tabs and have removed the count variable and its working properly now. Many apologies if you thought my question was unclear. And thanks.
Re: what on earth is going on?
by planetscape (Chancellor) on Nov 10, 2005 at 17:30 UTC

    I like the split "trick" mentioned here for retrieving only certain columns from an x-delimited file...

    HTH,

    planetscape
Re: what on earth is going on?
by ikegami (Patriarch) on Nov 10, 2005 at 17:24 UTC

    Replace
    foreach my $line (<SPEC>)
    with
    while (my $line = <SPEC>)
    to avoid loading the entire file into memory. This is a performance improvement, not a solution to your problem. Sounds like eric256 has already solved your problem.

Re: what on earth is going on?
by davidrw (Prior) on Nov 10, 2005 at 17:35 UTC
    Assuming $var1, $var2, and $var3 are all different, then this:
    if(($count != $var1) || ($count !=$var2) || ($count !=$var3))
    will always be true.

    Your call to system("chosespectraldata.pl $spectralfile $var1 $var2 $var3 > $diroutput$output"); can also be replaced with:
    my $cmd = q#perl -F, -aane 'chomp @F; print join ",\t", @F[ grep(! /^(# . "$var1|$var2|$var3" . q!)$/, 0..$#F)] ' ! . " $spectralfile > $diroutput$output "; warn $cmd; system( $cmd );
    see perlrun for details. also try this static cmd on your spectralfile:
    perl -F, -lane 'chomp @F; print join ",\t", @F[ grep(! /^(1|10|6)$/, 0 +..$#F)] ' blah.txt
Re: what on earth is going on?
by blazar (Canon) on Nov 10, 2005 at 17:24 UTC
    So, one file might not have columns 1 3 and 6

    What does that mean? ITYM you want to output a file missing any three columns.

    (Incidentally i wonder why you want to do that for all combinations of three columns.)

    I want to create files for all combinations of 10 columns .. making 120 files in total.
    How strange!! Especially since Choose(13,10)=Choose(13,3)=286. Whatever, I would go for some combinatorial module and then slicing.
      This code uses Math::Combinatorics to work out the combinations. It is easier to work out the combinations that you want to keep rather than those that you want to get rid of.

      use Math::Combinatorics; my @cols = (0..12); my @combin; my @data = ('A'..'Z'); my $combinations = Math::Combinatorics->new(count => 10, data => [@col +s]); while (@combin = $combinations->next_combination()) { print join("\t", @data[@combin]), "\n"; }