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?

I love it when things get difficult; after all, difficult pays the mortgage. - Dr. Keith Whites
I hate it when things get difficult, so I'll just sell my house and rent cheap instead. - perldigious

In reply to Syntax Question Related to "join" on a Complex Data Structure by perldigious

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.