in reply to Print something when key does not exist

(Longish) one-liner for this: (Line broken for readability ...)
perl -F/\\^/ -lanE 'next if $.==1 or !@F; $h{$F[0]}{$F[1]}=$F[2]; $v{$F[1]}++ } {say join ("^", "Name",@w=sort keys %v); say join ("^",$_,@{$h{$_}}{@w}) for sort keys %h' data1.txt
Update: FWIW, kcott's(++) detailed explanation below follows exactly the logic in my one-liner, that I was too lazy to document.

        What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?
              -Larry Wall, 1992

Replies are listed 'Best First'.
Re^2: Print something when key does not exist
by jaypal (Beadle) on Apr 06, 2014 at 01:11 UTC

    Thanks so much for this insane one-liner. I have learnt so much about awk by writing and reading awk one liners. This will help a great deal as well.

    Two questions:

    What does the following mean? Is that for handling blank lines?

    or !@F;

    How does, index increment for the array in the following to cover every element?

    say join ( "^", $_, @{ $h{$_} } {@w} ) for sort keys %h'
      ++ for asking good questions.

      Yes the !@F handles blank lines. When split, these will leave the @F array unpopulated, so scalar(@F) will be 0, or false, and the line gets skipped.

      The %h is a Hash of Hashes. the outer "for sort keys %h" handles the first (outer) hash.
      Inside that, I use a "hash slice" to get an array of values for keys specified by @w (which is a sorted array).

      This is pretty much the same thing kcott does in his line:

      map { $data{$name}{$_} || '' } @codes;
      except - my code does not worry about non-existing keys - since I run without 'warnings', my code does the right thing without complaint.

              What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?
                    -Larry Wall, 1992

        Thanks so much for your prompt response. Hash slice looks really handy. It effectively eliminated the need for two for loops I have in my answer.