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

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on Split a variable at a Given Line-Number

Replies are listed 'Best First'.
Re: Split a variable at a Given Line-Number
by moritz (Cardinal) on May 18, 2010 at 12:02 UTC
    Untested:
    use List::Util qw(max); my @lines = split /\n/, $str; my $prefix = join "\n", @lines[0..@lines-16]; my $postfix = join "\n", @lines[max(0, @lines-15)..$#lines];

    (Update: fixed offby1 error)

      slicing lists instead of arrays and using negative indices is even shorter:

      $txt=join "\n",0..99; print join "\n", (split /\n/, $txt, -1 )[-5 ..-1];

      OUT

      95 96 97 98 99

      Cheers Rolf

      UPDATE:

      This handles the case of #slicelines > #textlines

      print join "\n",grep {defined} (split /\n/,$txt,-1 )[-16 ..-1]

      UPDATE: added -1 to handle trailing empty lines, but I suppose thats not intended in this usecase.

        Hole-in-one! :)

        print ( (split /(\n)/,$txt )[-32..-1] )

        Cheers Rolf

        Update: added parens for print...

      No problems with that as of yet, works absolutely fine. Never seen max before, so thanks for showing me it. Cheers.
Re: Split a variable at a Given Line-Number
by cdarke (Prior) on May 18, 2010 at 12:47 UTC
    TMTOWTDI, here is my attempt:
    # $var is the original variable $var =~ /(.*)((?:^[^\n]*\n){15})\z/ms; my ($var1, $var2) = ($1, $2);
Re: Split a variable at a Given Line-Number
by LanX (Saint) on May 18, 2010 at 12:12 UTC
    ... prizes for the most simple, working piece of code!

    prizes for people showing instead of requesting code ...

    Cheers Rolf

      Agreed (see reply to anonymous user). Thank you for the code, very useful and nice of you to help.
Re: Split a variable at a Given Line-Number
by Anonymous Monk on May 18, 2010 at 12:31 UTC
    perlmonks isnt your code writing service, your bitch, your TA. Not even one scrap of code you've attempted. Go learn perl, stop posting your tasks here and trying to hook people in with so called 'challenges' or talk of reward.

      Sorry you've found the time to get annoyed about this thread (and perhaps others). I am learning Perl very nicely thank you and if you research a bit more into my user history you will see that most of the tasks are all on the same topic (one which I've been toying with for quite some time by myself). Whenever I have fully solved the problem I have also acknowledged the help and reproduced full working code for other people whom may have had a similar problem to use.

      As a new user of Perl I've had little to offer as of yet to other people's problems, as usually there are other users far more qualified to offer better solutions. "Challenges" and "Prizes" was simply to lighten up the topics a little, not to induce a false pretense into what I intended. I appreciate that altruism is major part of the functionality of this site and I do not intend to linger as a parasite for much longer. Furthermore, you don't have to reply to these threads. If you feel you would prefer not to offer support to a beginner then why not just not? Anyway, have a nice day and thanks again to everyone that has helped. All is working extremely well.

        ... don't feed the trolls! :)

        Cheers Rolf