perldigious has asked for the wisdom of the Perl Monks concerning the following question:
Due to my own programming vocabulary deficiencies, poor Google foo, or both I'm unable to search effectively for an answer to this syntax question. Sparing what I hope is unnecessary detail, I have in the past done the following and was amazed (not for the first time) that Perl let me do this and furthermore did exactly what I wanted.
my $sales_line = join "\t", @data[@sales_indexes{@sales_columns}];
Where I use the @sales_columns array to give me a hash slice of %sales_indexes (using @ instead of % for a list) which I use to take an array slice of @data, join it with tabs, and then store the result as the $sales_line string. Yeah... I think I said that all correctly. When this worked it was one of those, "damn I love Perl" moments for me. I often physically walk over to the only other Perl user I know and say exactly that whenever they happen.
Which brings me to my question, because it's somewhat similar, and it's a rare case of Perl not understanding what I meant and/or doing what I wanted, and I'm hoping the error is mine and I just have the exact syntax wrong. I tried the following which gives a syntax error.
my $transcripts_line = join "\t", @transcripts{$transcript_ip}{$transcript_id}{@transcripts_columns};
Where %transcripts is a 3D hash that I'm trying to use the same up front @ instead of % trick to get the value list I want from the hash slice that I was hoping would be provided by the @transcripts_columns array ($transcript_ip and $transcript_id are simple scalars). I attempted the below as well, which doesn't give a syntax error, but doesn't work as expected either.
my $transcripts_line = join "\t", ($transcripts{$transcript_ip}{$transcript_id}{@transcripts_columns});
The way I did solve my problem was as follows.
my $transcripts_line = join "\t", map {$transcripts{$transcript_ip}{$transcript_id}{$_}} @transcripts_columns;
Which is fine, but... the first line of code I gave that worked was just so, well... succinct and pretty. :-)
As I said, I'm hoping I've just messed up the syntax with what I originally tried and I don't actually have to resort to the map inside of a join to make this work.
Any thoughts from the monastery?
|
|---|