[Bay View Training] Perl classes in Dallas and Silicon Valley and a new Perl tip
William Ward - Bay View Training
william.ward at bayview.com
Mon Oct 20 21:20:30 PDT 2008
The Bay View Training newsletter has been quiet for a while but we're
bringing it back. Look for updates about once a month with news of upcoming
classes and Perl tips.
In this issue:
- Perl training in Plano TX (Dallas/Fort Worth area) on November 17-18
(Mon/Tue)
- Perl training in Sunnyvale CA (Silicon Valley / SF bay area) on
December 6 & 13 (Sat)
- Perl tip: Searching files with multi-line entries
Perl training in Plano TX (Dallas/Fort Worth area) on November 17-18
(Mon/Tue)
We will be holding Perl 101 and 201 classes in Plano, TX on Monday November
17 and Tuesday November 18. If you or someone you know is in the
Dallas/Fort Worth area and would like to learn Perl, this is your chance!
Please email enroll at bayview.com today for more information or check our Web
site at http://www.bayview.com/texas/
Perl training in Sunnyvale CA (Silicon Valley / SF bay area) on December 6 &
13 (Sat)
We also are going to be holding Perl classes here in California on Saturdays
in December: the 6th and 13th for Perl 101 and Perl 201 respectively. See
http://www.bayview.com/bayarea/ or send mail to enroll at bayview.com for more
information.
Perl tip: Searching files with multi-line entries
Say that you have a file that looks something like this:
2008-01-02: first entry
2008-02-03: second entry on two lines
here is the additional line
2008-03-04: third entry
has
three
extra lines
2008-04-05: fourth entry has just one on line again
If you need to search for all entries that have "line" in the text, and
display the entire entry when found, you can't just search line-by-line —
that would work for the first and fourth entries, but the second entry would
miss the additional line, and in the third entry the word "line" is on the
fourth line so you'd miss the first three.
What you need to do in a case like this is read line-by-line, but only
process an entry once you've found the end of the entry. There are two ways
to solve this, depending on your data and what your needs are:
If the file is not very large (and never will be), and you need to do the
search multiple times, then you could load the entire file into memory as an
array of entries, and then search that array using grep or foreach.
If the file is very large, or you only need to scan through it once to find
one result, then just load each entry into a string, and display that string
if it matches.
First I'll show how to load the entire file since I think it's easier to
understand:
my @stuff;
while (<IN>) {
if (/^\s/) { $stuff[-1] .= $_; }
else { push @stuff, $_; }
}
print grep { /line/ } @stuff;
If the line begins with space, then it's a continuation line, so modify the
previous entry found (the last item of the array, using index -1) to add the
text to it. If the line doesn't begin with space, it's a new entry so push
it onto the end of the array. Once the entire file is read, each element in
@stuff would correspond to one record, including the multiple extra lines,
so it's easy to scan using grep to find what you need.
The second approach involves using a scalar, rather than an array, to build
up each record. When the next new record starts, or end of file is reached,
we check to see if the record we've just read matches the pattern:
my $last_entry;
while (<IN>) {
if (/^\s/) {
$last_entry .= $_;
}
else {
print $last_entry if $last_entry =~ /line/;
$last_entry = $_;
}
print $last_entry if $last_entry =~ /line/ && eof(IN);
}
If you have any questions or feedback, reply by email or go to the blog
entry to post a comment:
http://www.bayview.com/blog/2008/10/20/searching-files-with-multi-line-entries/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.bayview.com/pipermail/training-announce/attachments/20081020/9cf5535a/attachment.html
More information about the Training-announce
mailing list