in reply to Re: Re: Re: Re: ascii manipulation in perl
in thread ascii manipulation in perl

I'll bite:
#!/usr/bin/perl -w use strict; my %file; while(<>) { my $type = substr($_,0,3); defined($file{$type)) or open($file{$type}, ">$type.out") or die "Couldn't open $type.out: $!"; print $file{$type}; }
Here's the run down:
  1. a hash to hold file handles
  2. while there's more records to read
  3. get the first 3 chars in the line and store them in a variable
  4. if we haven't already opened an output file called "type.out", do so
  5. print out the record we read to the apporopriate file

thor

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Re: ascii manipulation in perl
by CountZero (Bishop) on Aug 13, 2003 at 06:00 UTC

    Generally I rather use exists to test whether a hash-key exists. It avoids getting "false negatives" on hash-keys which might have undef as a value (which is not the case here, so your code works OK).

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law