Linux Rocks wrote: > > Hi All, Hello,
> I want to read contents of a dir, filtered, into a hash. I have this: > > #/usr/local/bin -w ^^^^^^^^^^^^^^ I see no 'perl' there. > use strict; > my (@ARRAY, %TEST, $FILTER) = ""; Why are you assigning "" to the first element of @ARRAY? > print "Enter filter:"; > $FILTER = <STDIN>; > chomp $FILTER; > @ARRAY = ` ls -l /foo/bar | grep "$FILTER" `; > for (@ARRAY) { > chomp; > } > > This gets just the info I want, but it's all run together as a single string in > each element. How can I get each portion of each element returned to hash with > filename as the key? I would like filename as key and separate values for each > field, ie. rights, size, owner, group, date. > > I'm guessing I can do something like this: > > for (@ARRAY) { > #magical split on whitespace and stuff into hash; > } > > But what is the magical split thingy? I tried %TEST = (split " "); > but that didn't seem to work as rights are the same on all files and since that > went in first as key, it just kept overwriting existing keys. I then tried > this, which I think was very close: > > for (@ARRAY) { > ($File, $Time, $Date, $Size, $Group, $Owner, $Rights) = (reverse split " "); > $TEST {$File} = $Time, $Date, $Size, $Group, $Owner, $Rights; > } > > but I am getting "Useless use of a variable in void context [...]". I know > there are much easier ways to manipulate file and dir data. I am doing this as > practice as I will eventually need to retreive data from a proprietary command > that will return data in a simlar manner and I need to be able to manipulate it. Perl has the tools to do most of that without running an external program like 'ls' or 'grep'. #/usr/local/bin/perl -w use strict; use POSIX 'strftime'; use Stat::lsMode 'format_mode'; my $dir = '/foo/bar'; print 'Enter filter: '; chomp( my $filter = <STDIN> ); opendir DIR, $dir or die "Cannot open $dir: $!"; my @array = grep /$filter/, readdir DIR; closedir DIR; my %test = map { my @stat = lstat "$dir/$_"; my $time = strftime '%T', localtime $stat[ 9 ]; my $date = strftime '%D', localtime $stat[ 9 ]; my $size = $stat[ 7 ]; my $group = getgrgid $stat[ 5 ]; my $owner = getpwuid $stat[ 4 ]; my $rights = format_mode $stat[ 2 ]; $_, [ $time, $date, $size, $group, $owner, $rights ]; } @array; John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]