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

Hi monks,

I want to realize such a function that it can cut some columns from one variable and returns them to an other variable, it also allows me to choose the columns and the separators arbitrary.

E.g. I want to extract the 2nd, 3rd and 5-7th columns from $source and save them into $dest, and the separators for the columns can be space, # or =.

Are there any easy ways to implement this function?

Thanks a lot!

Replies are listed 'Best First'.
Re: Perl Substr
by Happy-the-monk (Canon) on Jun 18, 2013 at 15:44 UTC

    Welcome back lddzjwwy!

    Maybe you'd rather reread your own thread Perl Substr and follow the fine advice the monks have given you there and here too: Perl $

    Especially the please "Start by trying and showing us what you have tried :)" part. That would be very wise nice!

    Cheers, Sören

    (hooked on the Perl Programming language)

Re: Perl Substr
by LanX (Saint) on Jun 18, 2013 at 16:03 UTC
    why do you use the same title for a different question?

    > Are there any easy ways to implement this function?

    yes see split, you'll find plenty of examples there and in the archives

    Try to apply hat you learn there and ask again - showing your efforts -if you have problems! =)

    Cheers Rolf

    ( addicted to the Perl Programming Language)

Re: Perl Substr
by Eily (Monsignor) on Jun 18, 2013 at 16:02 UTC

    You may need to be a little more explicit on what you're wanting to do. Mainly, a short example with input and desired ouput would be of much help.

    If by n-th column you mean the n-th character of each line, or the n-th value in a fixed length data input, you probably should have a look at unpack, perlpacktut might be a useful read.

    For the ouput, it looks like a job for join.

    Then as Happy-the-monk said, the not-so-easy-but-still-best-way to do it is to give it a try.

Re: Perl Substr
by CountZero (Bishop) on Jun 18, 2013 at 16:25 UTC
    Are there any easy ways to implement this function?
    Yes of course. Write a subroutine which takes the following parameters: the variable that contains one line of data, the separator and a list of numbers (zero based!) of the columns to return. The subroutine returns a list containing the selected fields which you can save in an array. The subroutine returns a string of the fields extracted, joined by the same separator.

    All you have to do now is write the body of this subroutine.

    See how easy it is:

    use Modern::Perl; sub extract { my ($data, $separator, @fields) = @_; return join $separator, (split(/$separator/, $data))[@fields]; } my $input = 'First#second#third#and this is field no. 4#FIVE5five5#all + sixes 666#Seventh and last'; my $extraction = extract($input, '#', 1, 3, 4, 6); say $extraction;
    The subroutine does not do any error checking of the input parameters and will not deal correctly with escaped separators or quoted fields.

    Update: I added a code example.

    Update 2: amended the code example to return a string rather than a list

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: Perl Substr
by hbm (Hermit) on Jun 18, 2013 at 16:14 UTC

    Not sure how you want to "save" multiple columns into a scalar, but consider:

    $_ = 'one two#three=four five=six#seven eight=nine'; print join '.', (/([^#= ]+)/g)[1,2,4..6]; #prints #two.three.five.six.seven