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

i wrote this part of a script earlier and was wondering something.

the line that is:
my ($temp, $needed_data_0) = split(/data_here(.+)/, $_);
i was wondering if there waqs a way to get just the data i needed without using an additional variable, eg just using $needed_data_0. im thinking maybe i use the wrong split pattern?

Replies are listed 'Best First'.
Re: split paragraph
by Corion (Patriarch) on Nov 02, 2014 at 21:34 UTC

    Just use my($needed_data)= /data_here(.+)/. See perlop on Regexp-Quote-Like Operators. There it is explained how to use the match operator in list context and what it returns.

      well i tried that, but it matched for some reason the beginning of the paragraph to the first newline char. say for instance i have:

      firstline
      secondline
      data_hereDATA

      the pattern "data_here" goes into $temp, and $needed_data is actually "DATA";
      if i wanted "DATA" but didnt know "DATA" was going to be "DATA" and could have been "abc" or "a1dhiud76", the regex i posted above (which i will need to go back and explain further in detail now that i think about it) will match everything AFTER "data_here" until new line char... which returns "DATA" or any other values/characters to $needed_data. im trying to do that without having to spend that extra variable.

      the solution given earlier wont work for some reason tho undef does. correction what corion said did indeed work. i was still trying to split for some reason. after removing the "split" from the function it works. thanks

        I'm sorry, but without seeing your code, and your data, the output you get and the output you want, I can't really help you there.

        Maybe you can explain things better if you post real, runnable code and data for that program, because for me, the following just works as I imagine it should:

        #!perl -w use strict; my $data= <<EOM; firstline secondline data_hereDATA THAT IS CAPTURED EOM $_= $data; my($result)= /^data_here(.+)/m; print $result; # DATA THAT IS CAPTURED
Re: split paragraph
by Eily (Monsignor) on Nov 02, 2014 at 21:42 UTC

    Corion's answer seems to be what you need here. But just for your information, you can ignore an unused value with the undef keyword: my (undef, $needed) = getAList();

Re: split paragraph
by AnomalousMonk (Archbishop) on Nov 02, 2014 at 23:12 UTC
      thank you for your input, i will try my beswt to remember that next time
Re: split paragraph
by choroba (Cardinal) on Nov 03, 2014 at 09:50 UTC
    You can only pick the appropriate value from the list:
    my $needed_data = (split /data_here(.+)/)[1];

    Note that $_ as the second argument to split can be omitted.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: split paragraph
by james28909 (Deacon) on Nov 02, 2014 at 22:29 UTC
    actually i have found that using my(undef, $data_needed)=split(/(.+)/,$_); works if you have nothing after the pattern match. if its just my($data_needed)=(/^(.+)/,$_); it will put the whole paragraph into $data_needed for some reason. but this is just for me, and someone else may be different

    thanks everyone for your input :)

      Your uses of the split and  m// built-in functions in this and related posts have a strong flavor of "elephants to catch eels" about them. Consider the following code, which operates on a test string containing embedded newlines because you seem to be using paragrep mode, implying such newlines:

      c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "$_ = qq{abc\ndef\nghi}; my @ra = split /(.+)/; dd \@ra; ;; @ra = /^(.+)/; dd \@ra; " ["", "abc", "\n", "def", "\n", "ghi"] ["abc"]
      Do you feel you have a fairly good grasp of what is happening in these examples? If not, time and effort invested in gaining such understanding is likely to be generously repaid.

        i have a good understanding of this example yes, but there is some things i still dont understand ofcourse. i am no seasoned professional as most of you know, and sometimes the perldocs scare me lol, so when i am able to take something out of my own script, and dull it down from 30 or 40 lines to 7-10 lines, it makes me want to share that incase any other "new" perl scripters might find it useful (or atleast the example might give them a better idea of a way they can do it)

        and i am always open to feedback. would you like to see the code that this replaces? this code dumbs down what i had before and will find the three words in the if statement (and in any order) in the paragraph. and if that is the paragraph you were looking for it should return what you need out of it trivially.

        but please do not look at me as some seasoned professional. i know alot more now than i did a few months ago when i really started to dive into perl, but i am still a long way away from being "good".

        EDIT: and yes there are many newline characters and blank lines throughout the file