CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
loop question?

 



effc
Novice

Nov 23, 2012, 1:04 PM

Post #1 of 5 (1723 views)
loop question? Can't Post

I wrote a script that will take a data point for one coordinate and create a file. Now I want to create a file per column with each of the row headers as well (including the data). Can't seem to figure it out in my script this is what I have so fa, but it's still doing the original operation.

#!/usr/bin/perl
# parse JnJ output file and create single data file for each animal id/probe

print "start\n";

my $file = "JnJ.txt";
my $rowCounter = 0;

my @animalNames;
my $numCol;


open (IN, "$file" || die);

#main objective to pop data from X/Y with headers, this just appears to define


while (my $line = <IN>)
{
if ($rowCounter < 1) #process first line


{
$rowCounter = $rowCounter + 1;
@animalNames = split('\t', $line); #array definition
$numCol = scalar(@animalNames); #scalar definition

}


#read first line = list of hybs
#scalar = number of hybs
#open a file for each hyb / add header line (PROBE, ID)
#make a hash for number and array name

elsif ($rowCounter > 0)
{

my $colCounter = 2;
my @probeData = split('\t', $line); #row
my $probeName = @probeData[0];
while ($colCounter < $numCol)


{
my $animalName = @animalNames[$colCounter];
my @animalIds = split('\|', $animalName);
my $animalId = @animalIds[2];
my $probeValue = @probeData[$colCounter];


open (OUT, ">>$animalId") or die("Cant open $animalId, $!");
for($rowCounter < 35131) #35131 is the amount of rows in file
{
print OUT "$animalName\n$probeName\t$probeValue";

close (OUT);
}
$colCounter = $colCounter + 1;

}


}

}
close (IN);



Thanks in advance!!


Laurent_R
Veteran / Moderator

Nov 23, 2012, 2:56 PM

Post #2 of 5 (1718 views)
Re: [effc] loop question? [In reply to] Can't Post

I don't understand what you are trying to do.

Can you please explain clearly what you want to do, provide a sample of your input file and provide the output that you are expecting for that input.


effc
Novice

Nov 24, 2012, 7:12 AM

Post #3 of 5 (1709 views)
Re: [Laurent_R] loop question? [In reply to] Can't Post

Yes good idea....



The original file is a giant tab delimited matrix.



The current output file looks like this:
$animalName
$probeName
$probeValue



What I need is for each output file to encapsulate an entire $animalName 's $probeName s and $probeValues:

$animalName

$probeName $probeValue

$probeName $probeValue

$probeName $probeValue

$probeName $probeValue

...etc for all 35000 rows.

Then the program would close that file and create another file for the next animal, and so forth.


Laurent_R
Veteran / Moderator

Nov 24, 2012, 10:42 AM

Post #4 of 5 (1700 views)
Re: [effc] loop question? [In reply to] Can't Post

OK, now I understand better what you want to do. I would still like to see a data sample so that I could try to run your program if needed.

After a quick look at your code, I can see a number of opportunities for improvement, but don't see anything blatantly wrong. So, my first question: is your program doing what you want? If not, what is wrong in your output? Or is it slow? Well, what is the problem you wish us to address?


effc
Novice

Nov 24, 2012, 12:30 PM

Post #5 of 5 (1698 views)
Re: [Laurent_R] loop question? [In reply to] Can't Post

I have uploaded an example file to run and also what I would like to recieve. I wrote the script initially to extract one data point (see above example) but now I would like all data points for each animal in an individual file, and I can't figure out how to rewrite my script to accomplish the task. I appreciate you taking the time to understand my problem, does that make more sense?
Attachments: AA13example.txt (60 B)
  examplefile.txt (0.18 KB)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives