1.11M Members

Access a string from the first statement from within the second statement

 
0
 

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;
 
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
 
0
 

Thanks d5e5! Creating the variable outside the loop was the missing element that made this work.

Question Answered as of 2 Years Ago by d5e5
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: