If your data set is small, and I guess it is as you are reading all of your lines into a single string, you could use
grep to just get the lines that match a regex alternation of what you want then use another
grep with a post-incremented hash so that you only get the one 's' line rather than all three.
use strict;
use warnings;
my %seen = ();
print
grep { ! $seen{$_} ++ }
grep { m{^s|foo|e f$} }
<DATA>
__END__
s
erartt
e p
s
foo
e f
s
adflkja
This produces
s
foo
e f
as you require. The more usual idiom for reading all lines of a file into a single string (slurping) is
my $lines = '';
{
local $/;
$lines = <DATA>;
}
which changes the default input record separator inside the scope of the code block to undef so that the whole of the file is read into $lines in one fell swoop.
I hope this is of use.
Cheers,
JohnGG
Update: I should have placed the regex alternation in a non-capturing group. As it is, it matches lines beginning with 's', lines containing 'foo' anywhere and lines ending with 'e f'. Correct pattern is m{^(?:s|foo|e f)$}.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.