Skip to content

Instantly share code, notes, and snippets.

@fletch
Created September 10, 2009 19:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fletch/184768 to your computer and use it in GitHub Desktop.
Save fletch/184768 to your computer and use it in GitHub Desktop.
#!/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