I have File Like this.
BOOK_Author AYN RAND, ERNEST HEMINGWAY, JOAN DIDION
+RAY BRADBURY, GEORGE R.R. MARTIN
+GILLIAN FLYNN
Famous_singer Linkinpark, Lilwayne, Nikiminaj,
+seanpaul, Akon, Tpain
+Emenim
DESIRED OUTPUT
AYN RAND,ERNEST HEMINGWAY,JOAN DIDION,RAY BRADBURY,GEORGE R.R.MARTIN,GILLIAN FLYNN
Linkinpark,Lilwayne,Nikiminaj,seanpaul,Akon,Tpain,Emenim
I need to match the AUTHORS and SINGERS from different lines and need to put them in a respective array say @author and @singer.
I tried alot. I couldnt do that so please anyone help me to solve this. | [reply] |
Have you reviewed your course material? I would expect that it covers everything you need to approach this problem.
Please help us give you better answers. Show us what code you have already written, and explain to us what it does and where it fails to do what you need.
Also review push, split and likely perlsyn.
| [reply] |
| [reply] |
The key to this kind of problem is that when the line begins with a "continue-command"(plus) you want to process the rest of the line using the last "state" you were in, but when it doesnt begin with a "continue-command"(plus) you want to capture the new state, process the rest of the line with the new state, and remember that new state as the last_state for any following "continue-command"(plus) lines. Something like this
my $last_state='';
while (my $line= <DATA>) {
chomp $line;
next unless ($line);
if (substr($line,0,1) eq '+') {
# if line starts with a plus use last_state to process
process($last_state,substr($line,1));
}
else {
# otherwise set last_state and process with it
my @parts=split(' ',$line,2);
$last_state=$parts[0];
process($last_state,$parts[1]);
}
} # line
sub process {
my $state=shift;
my $input=shift;
... do stuff based on $state and $input ...
} # process
| [reply] [d/l] |