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

Hi Monks,

I m trying to make a key by splitting a row separated by pipes.The key would be a concatenation of all the element values stored at indexes which are themselves in a list.

So after spliting i just want to pick those elements and join them with pipe and form my key.

How can i do that ?

my $indexes = join(",",@arrofIndices); my $prodKey = join("|",(split/\|/,$$prodRef[$a])[$indexes]);

Thanks

Replies are listed 'Best First'.
Re: Construct Key by split
by Tanktalus (Canon) on Sep 10, 2008 at 04:55 UTC
    my @elements = split /\|/,$$prodRef[$a]; my $prodKey = join ('|', @elements[@arrofIndices]);

    Could be done in one line, but why?

    Apparently, you're confusing code with text. You don't need to create a string with commas in it, you just want the list of indices as-is.

Re: Construct Key by split
by ikegami (Patriarch) on Sep 10, 2008 at 05:06 UTC

    The "EXPR" in "(split ...)[EXPR]" must be

    an expression that returns a list of indexes.

    In your code, you have

    an expression that returns source code that once compiled and executed returns a list of indexes.

    Solution:

    my $prodKey = join("|",(split/\|/,$$prodRef[$a])[@arrofIndices]);

    By the way, $$prodRef[$a] is more readable as $prodRef->[$a]. See Dereferencing Syntax.

      Thanks all for help !!!

Re: Construct Key by split
by GrandFather (Saint) on Sep 10, 2008 at 04:56 UTC

    Something like:

    use strict; use warnings; my @arrofIndices = (1, 3); my $str = 'Zeroth|First|Second|Third'; my $prodKey = join "|", (split /\|/, $str)[@arrofIndices]; print $prodKey

    Prints:

    First|Third

    Perl reduces RSI - it saves typing