Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Help Me to Sort out from this

by Nansh (Acolyte)
on Mar 17, 2017 at 03:48 UTC ( [id://1184951]=perlquestion: print w/replies, xml ) Need Help??

Nansh has asked for the wisdom of the Perl Monks concerning the following question:

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

@authors=AYN RAND,ERNEST HEMINGWAY,JOAN DIDION,RAY BRADBURY,GEORGE R.R.MARTIN,GILLIAN FLYNN

@singer=sLinkinpark,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 @authors and @singers. I tried alot. I couldnt do that so please anyone help me to solve this.

Replies are listed 'Best First'.
Re: Help Me to Sort out from this
by davido (Cardinal) on Mar 17, 2017 at 04:54 UTC

    That seems like an inadequate sample to give us a complete picture of what needs to be done.

    Also, since you tried a lot, it would be useful to us for you to demonstrate a Short, Self-Contained, Correct Example of the code you tried. 30 lines or less ought to be adequate for a complete example.

    With that information we will gain a better sense of what you are after, as well as of where you are stuck, and can assist you in getting over the hurdle.

    You received similar advice previously; to provide samples of the code you are working on. It's the right thing to do. It helps us to be more effective in helping you, and avoids our wasting time presenting tips that you already have learned. Without posting code we might also begin to conclude that you prefer to have us do your homework for you.


    Dave

Re: Help Me to Sort out from this
by Laurent_R (Canon) on Mar 17, 2017 at 10:04 UTC
    Sorry, I don't understand your question.

    Please provide at least the desired output with the sample input you've supplied.

    And show what you've tried.

Re: Help Me to Sort out from this
by madtoperl (Hermit) on Mar 17, 2017 at 13:12 UTC
    Hi Nansh
    Eventhough I did not understand your correct issue, assuming that you have some problem on taking the authors only from a line. May be you have to look into using split function for getting the desired output

    Having more sample data/expected output will help to provide more specific answers..
      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.

        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.

        I tried alot.

        Why not provide a couple of the algorithms which you tried? Someone might then be able to point out where you were going wrong. Perhaps even one of those algorithms was a valid one and it was merely your code which was flawed. But again, with neither algorithm nor code to view we will never know.

        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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1184951]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-05-26 18:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found