CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Appending columns to a file in Perl

 



spyke212
Novice

Oct 8, 2008, 3:19 AM

Post #1 of 3 (622 views)
Appending columns to a file in Perl Can't Post

Hi,

I am trying to append some columns of data to a file, however everytime I append a new column of data (written from an array) it just adds it to the bottom of the last column insted of beside it. Does anybody know how to write data to a file in a column by column basis?

Thanks in advance

S


shawnhcorey
Enthusiast


Oct 8, 2008, 5:08 AM

Post #2 of 3 (619 views)
Re: [spyke212] Appending columns to a file in Perl [In reply to] Can't Post


In Reply To
Hi,

I am trying to append some columns of data to a file, however everytime I append a new column of data (written from an array) it just adds it to the bottom of the last column insted of beside it. Does anybody know how to write data to a file in a column by column basis?

Thanks in advance

S


You have to add the column to the end of each line. That means you have to rewrite the entire file. Something like this:


Code
#!/usr/bin/perl -i 

use strict;
use warnings;

my @new_column = ();
# load the column array

while( <> ){
chomp;
sprintf "%-80s %s\n", $_, shift @new_column;
}


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


KevinR
Veteran


Oct 8, 2008, 11:13 AM

Post #3 of 3 (606 views)
Re: [spyke212] Appending columns to a file in Perl [In reply to] Can't Post

Tie::File should make this process easy. It allows you to edit a file as if it were an array. It also helps to post the code you have written so far, it might just need a small tweak to make it work but there is no way to tell if we can't see it.

See the Tie::File documentation for usage. Its a core module so your install of perl should have it.
-------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives