PERL - split() with space 'except' on last value

Hi there, i wonder if somebody could help

I have a file that i want to split out into a hash, but the delimiter is either a space or a comma but the last column needs to be able to include spaces ..

so for example my file

/opt/accounts 80,90,60,70 Accounts Team
/opt/finance 70,45,90,89 Finance Team

which I split out using into a hash by doing the following

open (IN, "/var/opt/file.cfg");
while (<IN>) {
        chomp();
        my ( $filesystem,$warning,$minor,$major,$critical,$team ) = split(/[ ,]/);

        $hash->{$counter} = {
                counter => $counter,
                filesystem => $filesystem,
                warning => $warning,
                major => $major,
                minor => $minor,
                critical => $critical,
                team => $team
        };

        $counter++;
}

but the problem is, because I have split() using commas and spaces, when it comes to the last value I want to capture, its pulling "Accounts" into the hash instead of "Accounts Team".

unfortunately I am unable to change the source file, but does anyone know how I can split using spaces and commas except for the last column ? (which incedentally will always be the 6th and last value in the file)

any help on this would be greatly appreciated

Try something like this..

while(<FILE>) {
        chomp;
        $_ =~ m/(.+)\s(\d+),(\d+),(\d+),(\d+)\s(.+)/;
        $hash->{$counter} = {
                counter => $counter,
                filesystem => $1,
                warning => $2,
                major => $3,
                minor => $4,
                critical => $5,
                team => $6
        };
        $counter++;
}

thats great, thanks a lot it works :slight_smile: ... so am I right in saying the the columns ($1, $2 etc) are determinied by the bracketted parts of the match statement ?

Yes. However its a good practice to add checks before assigning $1, $2 to make sure that the regexp above worked and those special variables were defined. The above script assumes that every line exactly matches the regexp given.