At 10:41 AM +0800 3/23/12, lina wrote:
On Fri, Mar 23, 2012 at 10:20 AM, John W. Krahn <jwkr...@shaw.ca> wrote:
 John W. Krahn wrote:


 $ echo "Hr 12
 0001 2
 0002 3
 0003 1
 Hr 13
 0001 2
 0002 3
 0003 1
 Hr 14
 0001 2
 0002 3
 0003 1" | perl -e'

 my ( @hours, %data );
 while ( <> ) {
    push @hours, $1 if /^hr\s*(\d+)/i;
    $data{ $1 }{ $hours[ -1 ] } = $2 if /^(\d+)\s+(\d+)/;
    }

 print join( "\t", "", @hours ), "\n";
 for my $row ( sort { $a <=> $b } keys %data ) {
    print $row;
    for my $col ( sort { $a <=> $b } keys %{ $data{ $row } } ) {
        print "\t$data{$row}{$col}";
        }
    print "\n";
    }
 '
 12 13 14
 0001 2 2 2
 0002 3 3 3
 0003 1 1 1


 Probably better:


 $ echo "Hr 12
 0001 2
 0002 3
 0003 1
 Hr 13
 0001 2
 0002 3
 0003 1
 Hr 14
 0001 2
 0002 3
 0003 1" | perl -e'

 my ( @hours, %data );
 while ( <> ) {
    push @hours, $1 if /^hr\s*(\d+)/i;
    $data{ $1 }{ $hours[ -1 ] } = $2 if /^(\d+)\s+(\d+)/;
    }
 print join( "\t", "", @hours ), "\n";
 for my $row ( sort { $a <=> $b } keys %data ) {
    print $row;
    for my $col ( @hours ) {

        print "\t$data{$row}{$col}";
        }
    print "\n";
    }
 '
        12      13      14
 0001    2       2       2
 0002    3       3       3
 0003    1       1       1



Here I use:


        push  @hours, $1 if(/Hr\b(.*)\b/) ;

        $data{$1}{$hours[-1]} = $2 if(/\b(.+)\b(.*)\b/);

it reported, how can I fix it:


By using the correct regex. You need to use \d or [0-9] in your regular expression rather than the dot character, which matches any character. You are matching and capturing the entire line with (.+), leaving nothing for the second capture group.



$ perl calen.pl
        12       13      14
Argument "0003 1" isn't numeric in sort at calen.pl line 23, <$_[...]> line 12. Argument "0002 3" isn't numeric in sort at calen.pl line 23, <$_[...]> line 12. Argument "0001 2" isn't numeric in sort at calen.pl line 23, <$_[...]> line 12.
Argument "Hr 12" isn't numeric in sort at calen.pl line 23, <$_[...]> line 12.
Argument "Hr 13" isn't numeric in sort at calen.pl line 23, <$_[...]> line 12.
Argument "Hr 14" isn't numeric in sort at calen.pl line 23, <$_[...]> line 12.
Hr 12   Hr 13   Hr 14   0001 2                  0002 3                  0003 1l


Thanks,



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


--
Jim Gibson
j...@gibson.org

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to