Dear all. 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]";"; }; Thanks, Aiguo -----Original Message----- From: Alden Meneses [mailto:[EMAIL PROTECTED] Sent: Monday, October 18, 2004 11:50 AM To: [EMAIL PROTECTED] Subject: Re: help on comparing lines in a text file thanks GH here is my updated code use strict; use warnings; my $file = 'C:\Documents and Settings\menesea\My Documents\alden712\ibd100\ibd100.txt'; open(IBDIN, "<$file") || die "cannot open $file $!"; while(<IBDIN>){ my @array_a = split ' ', <IBDIN>; my @array_b = split ' ', <IBDIN>; compare_array(); } close(IBDIN); sub compare_array { my (%seen, @bonly); @[EMAIL PROTECTED] = (); # build lookup table foreach my $item (@array_b){ push(@bonly, $item) unless exists $seen{$item}; } print "@bonly \n"; } I use komodo from activestate to help write my perl scripts. When I use the while loop it complains about the @array_a and @array_b in the subroutine thus I get a compile error when running. can someone tell me what I am doing wrong? TIA, Alden On Sat, 16 Oct 2004 22:34:59 +0200, Gunnar Hjalmarsson <[EMAIL PROTECTED]> wrote: > Alden Meneses wrote: > > here is my updated code but it is not my loops are not set correctly > > as I get nothing when i print to screen..... > > > > open(IBDINA, "<$file") || die "cannot open $file $!"; open(IBDINB, > > "<$file") || die "cannot open $file $!"; chomp(@list_a=<IBDINA>); > > chomp(@list_b=<IBDINB>); > > for ($a = 0; $a < @list_a; $a+=2){ > > @array_a=(split(/ /,$list_a[$a])); > > for ($b =1; $b < @list_b; $b+=2){ > > @array_b=(split(/ /,$list_b[$b])); > > @[EMAIL PROTECTED] = (); # build lookup table > > foreach $item ($array_b){ > > push(@bonly, $item) unless exists $seen{$item}; > > print @bonly; > > } > > } > > } > > > > close(IBDINA); > > close(IBDINB); > > Sorry to say it, but it looks terrible. ;-) > > First and foremost, you are not using strictures and warnings. Posting > non-working code to a mailing list, without having had Perl perform > some basic checks, is bad, bad, bad. > > Another thing is that you open two filehandles to the same file, and > unnecessarily complicates the assigning of the arrays. There is no > need for those outer loops. > > This is a simplification of your code: > > #!/usr/bin/perl > use strict; > use warnings; > > my $file = '/path/to/file'; > > open IBDIN, "< $file" or die "cannot open $file $!"; > my @array_a = split ' ', <IBDIN>; > my @array_b = split ' ', <IBDIN>; > close IBDIN; > > my (%seen, @bonly); > @[EMAIL PROTECTED] = (); # build lookup table > foreach my $item ($array_b){ > push(@bonly, $item) unless exists $seen{$item}; > print @bonly; > } > > __END__ > > Now, that code is not correct either. Actually, it doesn't even > compile since strictures are enabled, but just that fact illustrates > how using strict can help you detect a mistake. > > Hopefully the above will help you move forward, and concentrate on the > comparison part of your program. > > -- > > > Gunnar Hjalmarsson > Email: http://www.gunnar.cc/cgi-bin/contact.pl > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > <http://learn.perl.org/> <http://learn.perl.org/first-response> > > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response> -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>