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;
#!/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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.