Many thanks to Paul. His solution works.
-tir
On Thu, 10 May 2001, Paul wrote:
>
> --- "Tirthankar C.P" <[EMAIL PROTECTED]> wrote:
> >
> > Folks, I have a file like this:
> >
> > Asea Brown Boveri Ltd.
> > 02-Aug-1999 | 02-Aug-1999 | 399.05
> > 03-Aug-1999 | 03-Aug-1999 | 395.00
> > 04-Aug-1999 | 04-Aug-1999 | 426.5
> > 06-Aug-1999 | 06-Aug-1999 | 406.00
> > 31-Jul-2000 | 31-Jul-2000 | 203.00
> > 01-Aug-2000 | 01-Aug-2000 | 203.65
> > | Overall | 399.05 | 203.65
> > Asian Paints (India) Ltd.
> > 02-Aug-1999 | 02-Aug-1999 |
> > 03-Aug-1999 | 03-Aug-1999 |
> > 28-Jul-2000 | 28-Jul-2000 |
> > 31-Jul-2000 | 31-Jul-2000 |
> > 01-Aug-2000 | 01-Aug-2000 |
> > | Overall | 196.88 | 280.70
> > Associated Cement Cos. Ltd.
> > 02-Aug-1999 | 02-Aug-1999 | 196.10 |
> > 03-Aug-1999 | 03-Aug-1999 | 211.75 |
> > 04-Aug-1999 | 04-Aug-1999 | 224.80 |
> > 10-Aug-1999 | 10-Aug-1999 | 231.10 |
> >
> > I would like to break up this file into three parts, based on the
> > company name, i.e., so that the data following the stock name
> > (till the next stock), comes in that file. Any ideas?
>
> Hmm....
>
> my $dat; # just making memory space for the file
> open IN, $file or die $!; # assuming $file already has the name
> { local $/ = undef; # $/ is the input record seperator
> $dat = <IN>; # slurp it all in at once
> } # close the local() scope to restore $/
> my @sections = split /\n(?=[a-z])/i, $dat; # see below
>
> Notes:
> 1) I've never done this. It may not work. (lol)
> 2) If it does work, this will leave every section but the last one
> without it's trailing newline. (just FYI)
>
> Explanation:
>
> my @sections = split /\n(?=[a-z])/i, $dat;
>
> Obviously, the my() creates @sections, which will hold each section of
> the file in a seperate cell (with which you may then do as you please).
>
> split() is going to break $dat into the pieces that you want, based on
> the pattern.
>
> /\n(?=[a-z])/i means "find every newline followed by an alphabetic
> character". (?=$pat) is a zero-width lookahead, so that only newlines
> followed by letters count. If that's not exactly the effect you want,
> you could easily edit the pattern. =o)
>
> This is assuming the structure of the file is pretty uniformly
> consistent with the example you gave.
>
> Good luck,
> Paul
>
> =====
> print "Just another Perl Hacker\n"; # edited for readability =o)
> =============================================================
> Real friends are those whom, when you inconvenience them, are bothered less by it
>than you are. -- me. =o)
> =============================================================
> "There are trivial truths and there are great Truths.
> The opposite of a trival truth is obviously false.
> The opposite of a great Truth is also true." -- Neils Bohr
>
> __________________________________________________
> Do You Yahoo!?
> Yahoo! Auctions - buy the things you want at great prices
> http://auctions.yahoo.com/
>
Tirthankar C.Patnaik
Research Scholar
Indira Gandhi Institute of Development Research Ring:+91-22-840 0919/20/21
Goregaon East Off:- x593 Res: x675
Mumbai 400 065 Fax: 91-022-8402752
India. http://www.igidr.ac.in email:[EMAIL PROTECTED]
Never argue with an idiot. They drag you down to their level, then
beat you with experience. -dilbert
---------------------------------------------------------------------------