From: Uri Guttman on 16 Feb 2010 02:48 >>>>> "JWK" == John W Krahn <someone(a)example.com> writes: JWK> Patrick H. wrote: >> Hi, I am toying around with the directory operations chapter of >> "Learning Perl" and ran into a bit of a snag. >> >> I am trying to open a directory, output all the -f files to a @files >> array and all the -d files to a @folders array. The problem is it >> seems to only work for the first instance of readdir I use; they both >> work individually when I comment the other out, but when I have them >> both only the first array is populated. Do I need to closedir and then >> opendir again, or is there a way for me to reset the cursor (if that >> is even the right terminology)? JWK> perldoc -f rewinddir i recalled that was in there. but it isn't needed as you note below JWK> my ( @files, @folders ); JWK> while ( my $file = readdir C_DRIVE ) { JWK> push @files if -f "$folder/$file"; JWK> push @folders if -d "$folder/$file"; JWK> } i hate all the duplicate pushes. try this on for size (untested :): use File::Slurp ; push( @{ -d "$folder/$_" ? \@folders : \@files }, $_ ) for read_dir( 'C:' ); that assumes if it ain't a dir, it is a file. uri -- Uri Guttman ------ uri(a)stemsystems.com -------- http://www.sysarch.com -- ----- Perl Code Review , Architecture, Development, Training, Support ------ --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
From: bugbear on 16 Feb 2010 04:36 Uri Guttman wrote: >>>>>> "JWK" == John W Krahn <someone(a)example.com> writes: > > JWK> Patrick H. wrote: > >> Hi, I am toying around with the directory operations chapter of > >> "Learning Perl" and ran into a bit of a snag. > >> > >> I am trying to open a directory, output all the -f files to a @files > >> array and all the -d files to a @folders array. The problem is it > >> seems to only work for the first instance of readdir I use; they both > >> work individually when I comment the other out, but when I have them > >> both only the first array is populated. Do I need to closedir and then > >> opendir again, or is there a way for me to reset the cursor (if that > >> is even the right terminology)? > > JWK> perldoc -f rewinddir > > i recalled that was in there. but it isn't needed as you note below > > JWK> my ( @files, @folders ); > JWK> while ( my $file = readdir C_DRIVE ) { > JWK> push @files if -f "$folder/$file"; > JWK> push @folders if -d "$folder/$file"; > JWK> } > > i hate all the duplicate pushes. try this on for size (untested :): > > use File::Slurp ; > push( @{ -d "$folder/$_" ? \@folders : \@files }, $_ ) for read_dir( 'C:' ); Yikes. That's a triumph of ingenuity over clarity IMHO. BugBear
From: Uri Guttman on 16 Feb 2010 11:06 >>>>> "b" == bugbear <bugbear(a)trim_papermule.co.uk_trim> writes: b> Uri Guttman wrote: >>>>>>> "JWK" == John W Krahn <someone(a)example.com> writes: >> JWK> Patrick H. wrote: >> >> Hi, I am toying around with the directory operations chapter of >> >> "Learning Perl" and ran into a bit of a snag. >> >> >> I am trying to open a directory, output all the -f files >> to a @files >> >> array and all the -d files to a @folders array. The problem is it >> >> seems to only work for the first instance of readdir I use; they both >> >> work individually when I comment the other out, but when I have them >> >> both only the first array is populated. Do I need to closedir and then >> >> opendir again, or is there a way for me to reset the cursor (if that >> >> is even the right terminology)? >> JWK> perldoc -f rewinddir >> >> i recalled that was in there. but it isn't needed as you note below >> JWK> my ( @files, @folders ); JWK> while ( my $file = readdir C_DRIVE ) { JWK> push @files if -f "$folder/$file"; JWK> push @folders if -d "$folder/$file"; JWK> } >> >> i hate all the duplicate pushes. try this on for size (untested :): >> >> use File::Slurp ; >> push( @{ -d "$folder/$_" ? \@folders : \@files }, $_ ) for read_dir( 'C:' ); b> Yikes. That's a triumph of ingenuity over clarity IMHO. hence the :) before it. :) uri -- Uri Guttman ------ uri(a)stemsystems.com -------- http://www.sysarch.com -- ----- Perl Code Review , Architecture, Development, Training, Support ------ --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
From: Peter Scott on 16 Feb 2010 09:20 On Tue, 16 Feb 2010 02:48:23 -0500, Uri Guttman wrote: > i hate all the duplicate pushes. try this on for size (untested :): > > use File::Slurp ; > push( @{ -d "$folder/$_" ? \@folders : \@files }, $_ ) for read_dir( > 'C:' ); > > that assumes if it ain't a dir, it is a file. As long as we're trying to reduce duplication and maintain clarity and semantics, how about: use File::Slurp; my (@files, @folders); my $folder = 'C:'; for ( map { "$folder/$_" } read_dir $folder ) { my $dest = -d ? \@folders : -f ? \@files : ''; push @$dest, $_ if $dest; } or (equally untested, don't know if the glob pattern is right for a Windows box, am not near one): my (@files, @folders); for ( glob 'C:/*.*' ) { my $dest = -d ? \@folders : -f ? \@files : ''; push @$dest, $_ if $dest; } -- Peter Scott http://www.perlmedic.com/ http://www.perldebugged.com/ http://www.informit.com/store/product.aspx?isbn=0137001274
From: Randal L. Schwartz on 17 Feb 2010 12:44 >>>>> "Uri" == Uri Guttman <uri(a)StemSystems.com> writes: Uri> i hate all the duplicate pushes. try this on for size (untested :): Uri> use File::Slurp ; Uri> push( @{ -d "$folder/$_" ? \@folders : \@files }, $_ ) for read_dir( 'C:' ); Uri> that assumes if it ain't a dir, it is a file. OP said he was working through "Learning Perl". Probably hasn't gotten to references yet, so you just blew his test score. :) print "Just another Perl hacker,"; # the original -- Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 <merlyn(a)stonehenge.com> <URL:http://www.stonehenge.com/merlyn/> Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc. See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: FAQ 1.2 Who supports Perl? Who develops it? Why is it free? Next: saving old versions of file |