We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,145 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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;
2
Contributors
2
Replies
6 Days
Discussion Span
1 Year Ago
Last Updated
3
Views
Question
Answered
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

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.5169 seconds using 2.66MB