For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > April 2004 > joining lines









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 joining lines
Kimberly Schramm

2004-04-23, 4:30 am

I have a file that is 300+ lines long containing data for an event. each
event takes up 6 lines. I would like to join the 6 lines, so that each
event is only one line. Is this possible in perl?

Thanks for your help!

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"That's what alchemists do. They show that, when we strive to
become better than we are, everything around us becomes better,
too." -Santiago, The Alchemist, by Paulo Coehlo
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Charles K. Clarkson

2004-04-23, 4:30 am

Kimberly Schramm <schramm@earth.northwestern.edu> wrote:
:
: I have a file that is 300+ lines long containing data
: for an event. each event takes up 6 lines. I would
: like to join the 6 lines, so that each event is only
: one line. Is this possible in perl?

Yes, you can use the 'join' function for that.

perldoc -f join

HTH,

Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328

Jeff 'Japhy' Pinyan

2004-04-23, 5:41 am

On Apr 23, Kimberly Schramm said:

>I have a file that is 300+ lines long containing data for an event. each
>event takes up 6 lines. I would like to join the 6 lines, so that each
>event is only one line. Is this possible in perl?


Sure. Here's how I'd go about doing it:

open IN, "< file.txt" or die "can't read file.txt: $!";
open OUT, "> new.txt" or die "can't write new.txt: $!";

until (eof IN) {
# read 6 lines from IN and put them in @record
my @record = map scalar(<IN> ), 1 .. 6;

# remove their newlines
chomp @record;

# printing an array in quotes puts a space between each element
print OUT "@record\n";
}

close OUT;
close IN;

--
Jeff "japhy" Pinyan japhy@pobox.com http://www.pobox.com/~japhy/
RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/
CPAN ID: PINYAN [Need a programmer? If you like my work, let me know.]
<stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.

Jan Eden

2004-04-23, 1:32 pm

Jeff 'japhy' Pinyan wrote on 23.04.2004:

> # read 6 lines from IN and put them in @record
> my @record =3D map scalar(<IN> ), 1 .. 6;


How does this work?

In the map function you gave, the first argument is scalar, which takes onl=
y one argument and returns a line from INPUT in scalar context. But how are=
the numbers from the range operator applied to the expression scalar(<IN> )=
?

Thanks,

Jan
--=20
These are my principles and if you don't like them... well, I have others. =
- Groucho Marx
Charles K. Clarkson

2004-04-23, 1:32 pm

Jan Eden <lists@janeden.org> wrote:
:
: Jeff 'japhy' Pinyan wrote on 23.04.2004:
:
: > # read 6 lines from IN and put them in @record
: > my @record = map scalar(<IN> ), 1 .. 6;
:
: How does this work?
:
: In the map function you gave, the first argument is scalar,
: which takes only one argument and returns a line from INPUT
: in scalar context. But how are the numbers from the range
: operator applied to the expression scalar(<IN> )?

They're not applied. The range acts as a counter.

HTH,

Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328

Jan Eden

2004-04-23, 4:39 pm

Charles K. Clarkson wrote on 23.04.2004:

>Jan Eden <lists@janeden.org> wrote:
>:=20
>: Jeff 'japhy' Pinyan wrote on 23.04.2004:
>:=20
>: > # read 6 lines from IN and put them in @record
>: > my @record =3D map scalar(<IN> ), 1 .. 6;
>:=20
>: How does this work?
>:=20
>: In the map function you gave, the first argument is scalar,=20
>: which takes only one argument and returns a line from INPUT=20
>: in scalar context. But how are the numbers from the range=20
>: operator applied to the expression scalar(<IN> )?
>
> They're not applied. The range acts as a counter.


I knew that, but I forgot about the basic function of the angle operator, i=
=2Ee. to return the *next* line of the associated filehandle each time it =
is invoked.

How embarassing.

Thanks,

Jan
--=20
Common sense is what tells you that the world is flat.
Sponsored Links







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

Copyright 2008 codecomments.com