Home > Archive > PERL Beginners > October 2004 > Generate a perl list file
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Generate a perl list file
|
|
| Aiguo Li 2004-10-19, 3:55 pm |
|
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:aldenm@gmail.com]
Sent: Monday, October 18, 2004 11:50 AM
To: beginners@perl.org
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);
@seen{@array_a} = (); # 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 <noreply@gunnar.cc>
wrote:
> Alden Meneses wrote:
>
> 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);
> @seen{@array_a} = (); # 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: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
| |
| JupiterHost.Net 2004-10-19, 3:55 pm |
|
> 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.
Use strict and warnings :)
> print " { labels =\n";
> print "\t\t (\n";
> open IN "tag.txt";
That shoudl error out because there;s no comma after IN
or die $!;
> while (<IN> ) {
> @line = split(/\t/);
> print "\t v=""@line[0]";";
> print "l=""@line[1]";";
> print " p= "@line[2]";";
These would all fail because the "'s are not escaped, have you even
tried executing the code?
I think you want $line[n] instead of @line[n]
> };
>
> Thanks,
Try this:
#!/usr/bin/perl
use strict;
use warnings;
open IN, 'tag.txt' or die $!;
while (<IN> ) {
my ($v,$l,$p) = split /\s+/;
print qq(\t v="$v"\;l="$l"\; p= "$p"\;\n);
}
| |
| John W. Krahn 2004-10-19, 3:55 pm |
| [ 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
| |
|
| >
> 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";}
> );
> }
Hi,
Here is my little contribution. Im a newbie myself... I may not have
understood your question completely, but here is some code that writes to
a file in the format that you have described above.
thx,
radhika
========================================
=
#!/usr/bin/perl -w
use strict;
use diagnostics;
#{v="1.0"; name = "AAA"; p = "0.001"; },
#{v="0.9"; name = "BBB"; p = "0.003";},
#{v="0.8"; name = "CCC"; p = "0.004";}
my $file = "data.txt"; #file read from
my $outfile = "data_out.txt"; #file to write to
my @header = ("v", "name", "p");
open(DATA, "<$file") or die "Cannot Open - $!\n";
open(OUT, ">$outfile") or die "$!\n";
print OUT "labels=\n";
print OUT "\(\n";
while ( <DATA> ) { #while loop to read first line only
if( $_ =~ /(\d+)\s+(\w+)\s+(\d+)/ ) {
print OUT "\{$header[0]=\"$1\"\; ";
print OUT "$header[1]=\"$2\"\; ";
print OUT "$header[2]=\"$3\"\; \}\n";
}
}
|
|
|
|
|