Maire has asked for the wisdom of the Perl Monks concerning the following question:
Hello, Monks. I'm hoping that you can help me with what is probably quite a simple problem, but which is completely stumping me!
I'm trying to work with text files which are fairly disorganized in terms of structure, and which have been stored in a hash. What I want from each file is to extract the line of text which begins "title:#" and ends with a "#" and store this text in a scalar to be used later.
The problem arises because, within some of the text files, there are multiple lines which begin and end with "title#" and "#" respectively. What distinguishes the "titles" I want is that they only ever appear in each text file once, whereas the "titles" I do not want appear at least twice (but sometimes three or four times) in the same text file.
So this is the basic script that I am using, which prints out all the titles
The script above, obviously, prints out all 8 captured lines which begin with "title", but my desired output is:use warnings; use strict; my %mycorpus = ( a => "<blah blah blah blah title:#this is text I want 1# blah blah blah", b => "blah title:#this is text I do not want# blah title:#this is text I want 2# blah title:#this is text I do not want# blah", c => "blah blah title:#this is text I want 3# title:#this is text I do not want# title:#this is text I do not want# title:#this is text I do not want# blah", ); foreach my $filename (sort keys %mycorpus) { my $titles = ''; while ($mycorpus{$filename} =~ /title:#(.*?)#/g){ $titles = $1; print "$titles \n"; } }
this is text I want 1 this is text I want 2 this is text I want 3
I thought it might work to add a line in after the titles have been captured which (was supposed to!) remove all lines which appeared multiple times, but my attempts at this failed
use warnings; use strict; my %mycorpus = ( a => "<blah blah blah blah title:#this is text I want 1# blah blah blah", b => "blah title:#this is text I do not want# blah title:#this is text I want 2# blah title:#this is text I do not want# blah", c => "blah blah title:#this is text I want 3# title:#this is text I do not want# title:#this is text I do not want# title:#this is text I do not want# blah", ); foreach my $filename (sort keys %mycorpus) { my $titles = ''; while ($mycorpus{$filename} =~ /title:#(.*?)#/g){ $titles = $1; $titles =~ s/(\b\w+\b)(?:\s*\1)+/$1/g; print "$titles \n"; } }
I hope that makes sense: I have been working on this for about 9 hours now, so I'm a bit frazzled! Any help/pointers here would be very much appreciated! Thank you!
|
|---|