Limbic`s code is well written, but i take a different approach.
Assumptions:
a) USER, LOGIN and MORE is a kind of markup.
b) You want to process the data somehow before writing it back.
c) You are capable to write file in/output by yourself.
That leads to the following code:
#!/usr/bin/perl
use strict;
use warnings;
{
local $/ = ""; # paragraph mode
while ( <DATA> )
{
chomp;
print process_user($_);
}
}
sub process_user
{
my ($user, $login, @info) = split ("\n", shift);
$user =~ s/USER //;
$login =~ s/LOGIN //;
@info = map { s/^MORE //; $_ } @info;
#sample processing: add a star everywhere
return join "\n",
"__BEGIN__",
"USER *$user",
"LOGIN *$login",
(map { "MORE *$_" } @info),
"__END__\n";
}
__DATA__
USER 1
LOGIN date
MORE info
MORE info
USER 2
LOGIN date
MORE info
MORE info
That produces this output:
__BEGIN__
USER *1
LOGIN *date
MORE *info
MORE *info
__END__
__BEGIN__
USER *2
LOGIN *date
MORE *info
MORE *info
__END__
Update:
Corrected minor typo and
Limbic`s "s".
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.