in reply to Re^2: Read File into array and split
in thread Read File into array and split
I'm not sure how you think split can return a list of dirs for every line, especially since the lines containing "ms" don't contains dirs.
my main goal is to put the hostname in to $client and the dir into @dir and each client starts with ms.
"starts with" implies split probably isn't the right tool.
That you don't have a list of similar items implies split probably isn't the right tool.
Assuming the data is as you showed in in your original post, and assuming that the hostnames are in square brackets and you simply didn't bother to fix your post, the following should do the trick.
my $file = '/scripts/clients'; open(my $fh, '<', $file) or die("Unable to open file \"$file\": $!\n"); my $hostname; my @dirs; while (<$fh>) { chomp; if (/^\[/) { backup($client, \@dirs) if defined($hostname); s/^\[//; s/\]$//; $hostname = $_; @dirs = (); } else { s/^"//; s/"$//; push @dirs, $_; } } backup($client, \@dirs) if defined($hostname);
DATA is a special name. You shouldn't use it. It's bad to use global variables anyway.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Read File into array and split
by tokyobuddha (Novice) on Mar 07, 2008 at 09:39 UTC | |
by ikegami (Patriarch) on Mar 08, 2008 at 05:52 UTC |