Search code examples
perl

How do I split a string into undetermined number of parts and print the parts to different files


I have data which comprises of a location name and some risk levels from 1 to 4, for each of 4 weather parameters, which are 1)24hr rainfall, 2) 5-day total rainfall, 3) minimum temperature and 4) maximum temperature. On a day,there can be only risk level 1 (no risk) for all the four parameters, in which case there is nothing to process. Or there can be one, two, up to four to process.

I am able to determine if there is no processing required (i.e. risk level 1 every where).

A data sample is given in __DATA__ below.

I am able to determine how many weather elements have risk levels higher than 1 (from the file header), and therefore am able to make an array @Riskarray, from this header.

What I would like to do is to split each line and write in a separate file for each weather element, two columns (the location and the risk value). This is similar to How can I split a string on the first occurrence of a digit for which I was very ably assisted.

In the code snippet below, I have no idea how to proceed after the while statement.

#!/usr/bin/perl -w

use strict;
use warnings;

my @RiskArray=("H1dR", "H5dR", "Tmin", "Tmax");
my $arraynum=scalar @RiskArray;

my $hdr=<DATA>;
for my $i (0..$#RiskArray){
 while (my $line = <DATA>){

 }
}

__DATA__
Risk    H1dR  H5dR  Tmin  Tmax
Maputsoe, 2, 1, 3,  2
Butha-Buthe (Butha-Buthe District), 4, 3, 2, 1
Mohale's Hoek, 2, 2, 3, 4
Qacha's Nek, 2, 3, 1,  2

sample output will be four files, e.g. file "H1dRrv.txt" will contain

Maputsoe, 2
Butha-Buthe (Butha-Buthe District), 4
Mohale's Hoek, 2
Qacha's Nek, 2

and H5dRrv.txt

Maputsoe, 1
Butha-Buthe (Butha-Buthe District), 3
Mohale's Hoek, 2
Qacha's Nek, 3

Solution

  • I was able to get results from your DATA post.

    #!/usr/bin/perl
    use strict;
    use warnings;
    #use re;
    
    (undef, my @hdr) = split(/\s+/, <DATA>);
    
    my %data;
    my @area;
    
    while (my $line = <DATA>) {
        chomp $line;
        my ($area, @risk) = split(/,\s+/, $line);
        push @area, $area;
        for my $i (0..$#hdr) {
            push @{$data{$hdr[$i]}}, $risk[$i];
        }
        
    }
    
    my @fh;
    
    for my $hdr (@hdr) {
        open my $fh, '>', "$hdr.txt" or die "open failed: $!";
        push @fh, $fh;
    }
    
    for my $i (0..$#hdr) {
        my $out = $fh[$i];
        for my $j (0..$#area) {
            my $str = "$area[$j], $data{$hdr[$i]}[$j]\n";
            print $out $str;
        }
        close $out or die "close failed: $!";
    }
    
    __DATA__
    Risk    H1dR  H5dR  Tmin  Tmax
    Maputsoe, 2, 1, 3,  2
    Butha-Buthe (Butha-Buthe District), 4, 3, 2, 1
    Mohale's Hoek, 2, 2, 3, 4
    Qacha's Nek, 2, 3, 1,  2
    

    The files created were:

    C:\Old_Data\perlp>type H1dR.txt
    Maputsoe, 2
    Butha-Buthe (Butha-Buthe District), 4
    Mohale's Hoek, 2
    Qacha's Nek, 2
    
    C:\Old_Data\perlp>type H5dR.txt
    Maputsoe, 1
    Butha-Buthe (Butha-Buthe District), 3
    Mohale's Hoek, 2
    Qacha's Nek, 3
    
    C:\Old_Data\perlp>type Tmin.txt
    Maputsoe, 3
    Butha-Buthe (Butha-Buthe District), 2
    Mohale's Hoek, 3
    Qacha's Nek, 1
    
    C:\Old_Data\perlp>type Tmax.txt
    Maputsoe, 2
    Butha-Buthe (Butha-Buthe District), 1
    Mohale's Hoek, 4
    Qacha's Nek, 2
    
    C:\Old_Data\perlp>