[ Please do not top-post. TIA ]
[ Please TRIM your posts. TIA ]
[ Please don't reply to a previous post unless you are contributing to that thread. TIA ]
Li, Aiguo (NIH/NCI) wrote:
Dear all.
Hello,
I have a tab delimited file as follow:
V name p 1.0 AAA 0.001 0.9 BBB 0.003 0.8 CCC 0.004 .....
I need to convert the file into following format: { labels = ( {v="1.0"; name = "AAA"; p = "0.001"; }, {v="0.9"; name = "BBB"; p = "0.003";}, {v="0.8"; name = "CCC"; p = "0.004";} ); }
I have not been able to make the following code work yet and would like to hear your suggestion for a better option. The following is my thought at this point.
print " { labels =\n"; print "\t\t (\n"; open IN "tag.txt"; while (<IN>) { @line = split(/\t/); print "\t v=""@line[0]";"; print "l=""@line[1]";"; print " p= "@line[2]";"; };
This will work:
#!/usr/bin/perl use warnings; use strict;
open IN, '<', 'tag.txt' or die "Cannot open 'tag.txt' $!";
my ( $format, @data );
while ( <IN> ) { if ( /^\D/ ) { $format = sprintf '{%s="%%s"; %s = "%%s"; %s = "%%s";}', split; } elsif ( /^\d/ ) { push @data, sprintf $format, split; } }
print "{\tlabels =\n\t(\n\t\t", join( ",\n\t\t", @data ), "\n\t);\n}\n";
__END__
John -- use Perl; program fulfillment
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>