Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Generate a perl list file
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>


Report this thread to moderator Post Follow-up to this message
Old Post
Aiguo Li
10-19-04 08:55 PM


Re: Generate a perl list file

> 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);
}

Report this thread to moderator Post Follow-up to this message
Old Post
JupiterHost.Net
10-19-04 08:55 PM


Re: Generate a perl list file
[ 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

Report this thread to moderator Post Follow-up to this message
Old Post
John W. Krahn
10-19-04 08:55 PM


Re: Generate a perl list file
>
> 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";
}
}



Report this thread to moderator Post Follow-up to this message
Old Post
Rs
10-26-04 01:55 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:56 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.