[EMAIL PROTECTED] wrote:
> Hi All,
>
> I want to read contents of a dir, filtered, into a hash. I have this:
>
> #/usr/local/bin -w
> use strict;
> my (@ARRAY, %TEST, $FILTER) = "";
>
> 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; }
You need brackets around the list you're assigning:
$TEST{$File} = [ $Time, $Date, $Size, $Group, $Owner, $Rights ];
The brackets create a anonymous array.
This is easier to write as:
my @fields = reverse split;
my $file = shift @fields;
$TEST{$file} = [ @fields ];
But split() is probably not the right tool for parsing ls -l output, since
some of the columns may contain whitespace. unpack() is probably better.
>
> 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.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]