For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > November 2005 > From column to row?









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 From column to row?
Andrej Kastrin

2005-11-28, 6:57 pm

Hi, I am totally NOOB in Perl and here is my first problem, which I
couldn't solve...
I have column data in file xy.txt, which looks like:
AAAAA
BBBBB
CCCCC
ABCDD
...
..

Now I have to transform this to row data file in the following way:
"AAAAA","BBBBB","CCCCC","ABCDD"

Is that possible?

Thanks in advance, Andrej
Chris Devers

2005-11-28, 6:57 pm

xicheng

2005-11-28, 6:57 pm

you may try the following command line: (under Linux)

perl -le '$"=qq(",");chomp(@a=<> );print qq("@a")' xy.txt

XC

Andrej Kastrin wrote:
> Hi, I am totally NOOB in Perl and here is my first problem, which I
> couldn't solve...
> I have column data in file xy.txt, which looks like:
> AAAAA
> BBBBB
> CCCCC
> ABCDD
> ..
> .
>
> Now I have to transform this to row data file in the following way:
> "AAAAA","BBBBB","CCCCC","ABCDD"
>
> Is that possible?
>
> Thanks in advance, Andrej


Timothy Johnson

2005-11-28, 6:57 pm


It's definitely possible. The first thing to do is define your problem
a little better. Do you want four items per row, or do you want to
slurp the entire file and make one long comma-separated list?

This is a good starting project because it makes you learn how to open,
read, and write to files. =20

Here's a little snippet to get you started:



###############################
use strict;
use warnings;

open(INFILE,"<xy.txt") or die("Couldn't open 'xy.txt' for reading!\n");
open(OUTFILE,">xy.csv") or die("Couldn't open 'xy.csv' for writing!\n");

#reading and writing
while($_ =3D <INFILE> ){
chomp $_;
#do something with $_...
print OUTFILE "This will be written to 'xy.csv'.\n";
}

###############################

Also check out:

perldoc -f open
perldoc -f print

You should be able to figure out how to rearrange it into the
appropriate format. If you have any trouble, post your code here and we
can help you.





-----Original Message-----
From: Andrej Kastrin [mailto:andrej.kastrin@siol.net]=20
Sent: Monday, November 28, 2005 11:45 AM
To: beginners@perl.org
Subject: From column to row?

Hi, I am totally NOOB in Perl and here is my first problem, which I=20
couldn't solve...
I have column data in file xy.txt, which looks like:
AAAAA
BBBBB
CCCCC
ABCDD
...
..

Now I have to transform this to row data file in the following way:
"AAAAA","BBBBB","CCCCC","ABCDD"

Is that possible?

Thanks in advance, Andrej


Andrej Kastrin

2005-11-28, 6:57 pm

Chris Devers wrote:

>On Mon, 28 Nov 2005, Andrej Kastrin wrote:
>
>
>
>
>Yes, it's possible.
>
>However, your description of the result you want doesn't seem to match
>with the subject line you used -- I was expecting that you want output
>like "ABCA", "ABCB..", "ABCD.", "ABCD", "ABCD". Which is it?
>
>Regardless, either way is possible.
>
>What have you tried so far?
>
>Anything?
>
>We can only help critique code you've attempted yourself.
>
>This is not a free script writing service.
>
>
>
>
>

I suppose that above example is not good one; here is the real one:
Mark
Francesco
Ann
Robert

transform to: "Mark","Francesco","Ann","Robert"

and here is my code:

$source_file = "input.txt";
$result_file = "output.txt";

open (SOURCE, $source_file) || die "cannot open $source_file: $!";
open (RESULT, ">$result_file") || die "cannot open $result_file: $!";

while (<SOURCE> )
{
my($line) = $_;
if ($line =~ $) #match to end of line
{
print "$line"; #how to make e.g. "Mark",
}
}
Jeff 'japhy' Pinyan

2005-11-28, 6:57 pm

On Nov 28, Andrej Kastrin said:

> I suppose that above example is not good one; here is the real one:
> Mark
> Francesco
> Ann
> Robert
>
> transform to: "Mark","Francesco","Ann","Robert"


> while (<SOURCE> )
> {
> my($line) = $_;
> if ($line =~ $) #match to end of line


You don't need a regex here at all, and even if you did, the contents of
the regex need to be inside /.../.

> {
> print "$line"; #how to make e.g. "Mark",
> }
> }


In your case, you want to do something like:

while (<SOURCE> ) {
chomp; # to remove the newline at the end of the string in $_
print qq{"$_",};
}

The qq{...} is a double-quoted string; that is, it's the same as "..."
except that it doesn't use " to delimit the string, but rather { and }.

This isn't a perfect solution for you, though, because it keeps a trailing
comma that I'm quite sure you don't want. But this is a start for you.

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://www.perlmonks.org/ % have long ago been overpaid?
http://princeton.pm.org/ % -- Meister Eckhart
Jose J. Cintron

2005-11-28, 6:57 pm

Here's a fixed version, not much though into it, you can figure out how
to remove the trailing ,=20

$source_file =3D "input.txt";
$result_file =3D "output.txt";

open (SOURCE, $source_file) || die "cannot open $source_file: $!";
open (RESULT, ">$result_file") || die "cannot open $result_file: $!";

while (<SOURCE> )
{
chomp; #Delete the NL character

# If you don't want the quotation marks in your output remove the \"
from the next line
print RESULT "\"$_\","; # You opened the RESULTS file but did not
write to it...
}
close SOURCE; # Just make sure you close files after you are done with
them...
close RESULT;


+------------------------------------------
| Jos=E9 J. Cintr=F3n
+------------------------------------------=20

> -----Original Message-----
> From: Andrej Kastrin [mailto:andrej.kastrin@siol.net]=20
> Sent: Monday, November 28, 2005 15:24
> To: Perl Beginners List
> Subject: Re: From column to row?
>=20
> Chris Devers wrote:
>=20
[color=darkred]
> seem to match=20
> want output=20
> I suppose that above example is not good one; here is the real one:
> Mark
> Francesco
> Ann
> Robert
>=20
> transform to: "Mark","Francesco","Ann","Robert"
>=20
> and here is my code:
>=20
> $source_file =3D "input.txt";
> $result_file =3D "output.txt";
>=20
> open (SOURCE, $source_file) || die "cannot open $source_file:=20
> $!"; open (RESULT, ">$result_file") || die "cannot open=20
> $result_file: $!";
>=20
> while (<SOURCE> )
> {
> my($line) =3D $_;
> if ($line =3D~ $) #match to end of line
> {
> print "$line"; #how to make e.g. "Mark",
> }
> }
>=20
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org For=20
> additional commands, e-mail: beginners-help@perl.org=20
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>=20
>=20
>=20

Gerard Robin

2005-11-28, 6:57 pm

On Mon, Nov 28, 2005 at 08:45:14PM +0100 Andrej Kastrin wrote:
> Hi, I am totally NOOB in Perl and here is my first problem, which I
> couldn't solve...
> I have column data in file xy.txt, which looks like:
> AAAAA
> BBBBB
> CCCCC
> ABCDD
> ..
> .
>
> Now I have to transform this to row data file in the following way:
> "AAAAA","BBBBB","CCCCC","ABCDD"
>

try this one:


#!/usr/bin/perl
# colrow1.pl

use strict;
use warnings;

my $source_file = "input.txt";
my $result_file = "output.txt";

open (SOURCE, $source_file) || die "cannot open $source_file: $!";
open (RESULT, ">$result_file") || die "cannot open $result_file: $!";

$/ = undef;
my $file = <SOURCE>;

my %hash = split /\n/, $file;

foreach (sort keys %hash) {
print RESULT "$_ , $hash{$_}\n";
}
close SOURCE;
close RESULT;

hth
--
Gérard

John W. Krahn

2005-11-29, 3:56 am

Andrej Kastrin wrote:
>
> I suppose that above example is not good one; here is the real one:
> Mark
> Francesco
> Ann
> Robert
>
> transform to: "Mark","Francesco","Ann","Robert"
>
> and here is my code:
>
> $source_file = "input.txt";
> $result_file = "output.txt";
>
> open (SOURCE, $source_file) || die "cannot open $source_file: $!";
> open (RESULT, ">$result_file") || die "cannot open $result_file: $!";
>
> while (<SOURCE> )
> {
> my($line) = $_;
> if ($line =~ $) #match to end of line
> {
> print "$line"; #how to make e.g. "Mark",
> }
> }


This should do what you want (untested):

use warnings;
use strict;

my $source_file = 'input.txt';
my $result_file = 'output.txt';

open SOURCE, '<', $source_file or die "cannot open $source_file: $!";
open RESULT, '>', $result_file or die "cannot open $result_file: $!";

while ( my $line = <SOURCE> ) {
chomp $line;
print qq/"$line"/, eof SOURCE ? "\n" : ',';
}

__END__



John
--
use Perl;
program
fulfillment
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com