From: [EMAIL PROTECTED]
> What I am trying to do is to take lines out of a file that are
> formatted like:
>
> SYSTEM="value1" DOMAIN="value2" etc.
>
> There are about 1000 lines like that and what I need to do is to take
> all of the keys (like system and domain) and put them into a hash. But
> each of those keys has multiple values that it can have. I need to
> then stick the values that each one has throughout the file into an
> array, and put the array into the hash. It will be like this:
>
>
> KEYS VALUES
> system [value1, value2, value3...]
> domain [value1, value2, value3...]
>
> I then need to print them out. How would I go about doing this?
Assuming the variable names are words and the values are always
quoted and do not span several lines you could do something like
this:
while (<FILE>) {
while (/(\w+)="([^"]*)"/g) {
push @{$data{$1}}, $2;
}
}
The outer while reads the file line by line and puts the lines into
$_. The regexp looks up things that look like XXXX="annything" in the
lines. The push needs better explanation I guess.
The regexp sets the $1 to the name of the key and $1 to the value,
$data{$1} is therefore the value(s) for the key $1.
@{$data{$1}} assumes there is an array reference in the hash %data
for key $1 and if the key doesn't exist yet it autovivifies the array
(creates an empty array and puts a reference to it int $data{$1}).
The push() then adds the value $2 into that array.
This creates the data structure you wanted.
Printing should be easy then. Just look at the "foreach" loop and
"keys" operator in
perldoc perlsyn
HTH, Jenda
===== [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]