#!/usr/local/bin/perl5 -w
# one possible solution to 3.3
# field codes 
#   I = Student ID
#   N = Name
#   E = Email
#   M = Major
#   L = Level

#   %data  is global
#   the special variable $. gives the line number; you hadn't had that

# ===================== subroutines ================================

sub reset
{
    undef %data;
}

sub get_data_from_line
{
    /^\s*([A-Z])\s*\:(.*)/ || die "*** unrecognized at line $.: $_";
    my ($field_code,$field_value) = ($1,$2);
    $field_code =~ /[INEML]/ ||
        die "*** unrecognized code $field_code, line $.\n";
    !defined($data{$field_code}) ||
        die "*** field $field_code given twice at line $.\n";
    $field_value =~ s/^\s*//;               # strip on left
    $field_value =~ s/\s*$//;               # strip on right
    $field_value =~ s/\(no information\)//; # make empty string if no info
    $data{$field_code} = $field_value;
}

sub show_group
{
    return unless defined %data;
    my @values;

    foreach $field_code ('I','N','E','M','L')
    {
        defined($data{$field_code}) ||
            die "*** no data given for field $field_code, before line $.\n";
        push(@values, $data{$field_code});
    }
    print join(':', @values),"\n";
}

# ======================== main part ================================

while(<>)
{
    if (/\S/)
    {
        &get_data_from_line;
    }
    else
    {
        &show_group;
        &reset;
    }
}
&show_group;

# ========================= Comments =================================
#
# 1.  The part for getting data from a nonblank line has been made
#     a separate subroutine so you can see the overall logic better.
#
# 2.  ``reset'' follows ``show_group'' immediately, because as soon
#     as the data has been printed out it isn't needed anymore. 
#
# 3.  For a more compact version we could move the ``undef data'' to
#     the end of the ``show_group'' subroutine and not have a separate
#     ``reset'' subroutine.  We could also not have a separate
#     subroutine ``get_data_from_line''.  This would be shorter but
#     not as clear.
#
# 4.  ``show_group'' is called for every blank line and at the end of
#     file, which may be more often than needed (for example, if the
#     last line of the file is blank), but that doesn't matter since
#     ``show_group'' starts with a test that keeps it from doing anything
#     if there has already been a ``reset''.
