You have specified an empty prototype by adding () after the subroutine name, but your subroutine actually takes an argument. Don't use prototypes unless you really need them. (In other words, leave off those parens.)sub SortDirectory() { my $arrayRef = $_[0];
grep is intended to be used for pulling elements from a list. Using grep on a scalar is silly. Additionally, you're comparing strings with the numeric comparison operator != rather than the string comparison operator ne.my @fileArray; my $currIndex = 0; my $i; for ( $i = 0; $i < @$arrayRef; $i++ ) { if ( (grep /\./, $$arrayRef[$i]) != "" )
This would be more Perlish: if ($arrayRef->[$i] =~ /\./)
{ $fileArray[$currIndex++] = splice(@$arrayRef,$i--,1); } } push (@$arrayRef, @fileArray); }
Unfortunately, your code is really just guessing as to whether each item is a file or a directory (you're clearly on a Windows machine; this /\./ test wouldn't work at all under Unix or MacOS). Instead, you should use the file test operators -d and/or -f to determine whether each item is a directory or a file:
Because it is necessary for the filetest operator to know where the file is, I am passing in the directory path as well as the list of contents.sub SortDirectory { my($path, $arrayRef) = @_; my(@dirArray, @fileArray); foreach (@$arrayRef) { if (-d "$path/$_") { push @dirArray, $_; } else { push @fileArray, $_; } } }
In reply to Re: Using grep with wildcards
by chipmunk
in thread Using grep with wildcards
by Sherlock
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |