Skip to content

Instantly share code, notes, and snippets.

@fletch
Created Sep 10, 2009
Embed
What would you like to do?
#!/opt/local/bin/perl
##
## Slightly more idiomatic and optimized Perl of 2num.pl from
## http://github.com/brendano/awkspeed/tree/master
##
use strict;
use warnings;
open( my $vocab_fh, '>', "vocab" ) or die "vocab: $!\n";
my %jmap;
my %i_file;
my $j_ctr = 0;
for my $infile ( @ARGV ) {
print STDERR "new file $infile\n";
open( my $in_fh, '<', $infile ) or die "$infile: $!\n";
open( my $out_fh, '>', "${infile}n" ) or die "${infile}n: $!\n";
my %imap;
my( $ival, $jval );
while( <$in_fh> ) {
my( $item, $feat, $val ) = split( / / );
$ival = ($imap{$item} ||= ++$i_file{ $infile });
print $vocab_fh qq{$feat\n} unless exists $jmap{ $feat };
$jval = ($jmap{$feat} ||= ++$j_ctr);
print $out_fh qq{$ival $jval $val};
}
}
__END__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment