#! /usr/local/bin/perl -w use strict; use warnings; use File::Path 'mkpath'; my %fd; my $root = 'cache'; $|=1; while (<>) { chomp; my @arr = split //, $_, 4; my $fd; if (@arr < 4) { if (!$fd{1}{$arr[0]}) { my $dir = "$root/$arr[0]"; mkpath $dir; open $fd{1}{$arr[0]}, '>', "$dir/list.txt" or die "open $dir/list.txt"; } $fd = $fd{1}{$arr[0]}; } else { if (!$fd{n}{$arr[0]}{$arr[1]}{$arr[2]}) { my $dir = "$root/$arr[0]/$arr[1]/$arr[2]"; mkpath $dir; open $fd{n}{$arr[0]}{$arr[1]}{$arr[2]}, '>', "$dir/list.txt" or die "open $dir/list.txt"; } $fd = $fd{n}{$arr[0]}{$arr[1]}{$arr[2]}; } print $fd "$_\n"; print "." unless $. % 5000; }