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

Hi Brethren,
The following code
print join "\n", map {"$_->[0]\t " . join ' ', @{$_->[1]}} @UnNum;
Prints the following data to screen
91 50, This is the first line 56 46, This is my second line
I would like to print this data to file omitting the first numbers so that my data is
50, This is my first line 46, This is my second line

Any ideas much appreciated
Many Thanks

Replies are listed 'Best First'.
Re: Printing Map to file
by lima1 (Curate) on Mar 25, 2006 at 20:50 UTC
    1. if you post code that uses some complex data structures, provide an example, e.g.
    my @UnNum = ( [ 0, [1,2,3] ], [ 0, [1,2,3] ]);
    2. if you don't understand some code others post here, try to read the perldoc (e.g. perldoc -f map) and then ask again if sth is still unclear.

    what this join/map line does (from inner to out):

    join ' ', @{$_->[1]}
    Simply generate a string with all elements in the second element (u know what i mean ;) ), the arrayref, seperated by blank.
    map {"$_->[0]\t " . join ' ', @{$_->[1]}} @UnNum;
    now generate an array with the same number of elements like @UnNum. the elements are now a string where the first array element is seperated by a tab from the string generated with join.
    print join "\n", map {"$_->[0]\t " . join ' ', @{$_->[1]}} @UnNum;
    now generate a string out of this new array, seperated by newline and print it.

    if you understand this, then you know the answer to your question.

      That's just what I was hoping for.
      Too often the answers just give a bit of code: no comment! I know it takes a lot longer to comment the code but it makes a big difference to someone like me who is new to programming.
      Thanks again for your time. I now understand what the code was doing.
Re: Printing Map to file
by explorer (Chaplain) on Mar 25, 2006 at 20:37 UTC
    print join "\n", map { join ' ', @{$_->[1]}} @UnNum;