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

Hi Monks.why split function need $_? ,i want to delete first column of my data.

Replies are listed 'Best First'.
Re: split function
by jethro (Monsignor) on Jul 28, 2011 at 17:03 UTC

    split does not need $_, $_ is just the default, i.e. it is the variable it uses if you don't specify one.

    @x= split /,/; #splits $_ @x= split /,/,$nod; #splits $nod
Re: split function
by toolic (Bishop) on Jul 28, 2011 at 17:21 UTC
    Additionally, the docs for split are available at your command prompt:
    perldoc -f split
Re: split function
by davido (Cardinal) on Jul 28, 2011 at 17:31 UTC

    Let's say that the first column is delimited with a comma, and there could never be another comma embedded within the first column:

    perl -pi.bak -e '$_ = ( split /,/, $_, 2 )[1];' myfile.txt

    That works by splitting each line into two parts; the part that comes before the comma, and the part that comes after. The comma is gobbled up. Then take only the second part, and return it back to $_ for output.

    You could do it with s/// too:

    perl -pi.bak -e 's/^[^,]*,//' myfile.txt

    Here we match everything from start of line, up to and including the first comma, and substitute that match with '' (nothing).


    Dave

Re: split function
by locked_user sundialsvc4 (Abbot) on Jul 29, 2011 at 12:14 UTC

    Perl is very big on “brevity.”   You can “say a lot with a little,” if you want to.   One of the ways that you can do it is through these implied, pre-defined variables.   See perlvar.

    You are not obliged to use $_, but you will encounter code that does.   Lots of it.   “Whatever you choose to do, just make it abundantly clear.”