Access a string from the first statement from within the second statement
Basically I am reformatting a list that looks like this
ko10101
K01392
K09134
ko34231
K05789
ko13452
K04665
K07881
into a csv file that looks like this:
ko10101\tK01392
ko10101\tK01392
ko10101\tK09134
ko34231\tK05789
ko13452\tK04665
ko13452\tK07881
while (<OUT>) {
my $line = $_;
chomp;
if ($line =~ /^$/) {
next;
}
elsif ($line =~ /ko/) {
# $test = 1;
my $pathway = \$line;
# print "$pathway\t";
}
elsif ($line =~ /K/) {
trim($line);
print OUT2 "$$pathway\t$line\n";
}
else {
print OUT2 "problem with $line\n";
}
}
close OUT;
bio-grad
Junior Poster in Training
50 posts since Oct 2010
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0
#!/usr/bin/perl;
use strict;
use warnings;
my $pathway;#To save string from previous iteration, create variable outside loop
while (my $line = <DATA>) {
chomp($line);
if ($line =~ /^$/) {
next;
}
elsif ($line =~ /ko/) {
$pathway = $line;
}
elsif ($line =~ /K/) {
#trim($line); #What does your trim() subroutine do?
print "$pathway\t$line\n";
}
else {
print "problem with $line\n";
}
}
__DATA__
ko10101
K01392
K09134
ko34231
K05789
ko13452
K04665
K07881
Outputs
ko10101 K01392
ko10101 K09134
ko34231 K05789
ko13452 K04665
ko13452 K07881
d5e5
Practically a Posting Shark
831 posts since Sep 2009
Reputation Points: 162
Solved Threads: 163
Skill Endorsements: 1
Thanks d5e5! Creating the variable outside the loop was the missing element that made this work.
bio-grad
Junior Poster in Training
50 posts since Oct 2010
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0
Question Answered as of 1 Year Ago by
d5e5