my @sections = split /\n(?=[a-z])/i, $dat;
I have a doubt here. perldoc -f split says,
------------------------------------------------------
If the PATTERN contains parentheses, additional array
elements are created from each matching substring in the
delimiter.
split(/([,-])/, "1-10,20", 3);
produces the list value
(1, '-', 10, ',', 20)
-----------------------------------------------------
What will this zero width lookahead assertion create? AFAIK, (?=... ) does
not consume anything.
------- Thanks in advance,
Atul
@k = map qw< 65 116 117 108 95 75 104 111 116 64 105 50 46 99 111 109 10
>, $m = sub { sub { sub { map { print chr } @{$_[0]}; +shift @{$_[0]} } }
}; @k && 1 while $m->()->()->( \@k )
Paul <[EMAIL PROTECTED]>
05/10/01 02:52 PM
Please respond to Hodges
To: "Tirthankar C.P" <[EMAIL PROTECTED]>
cc: [EMAIL PROTECTED]
Subject: Re: Breaking up a file.
--- "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/