最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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

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
Share edited Feb 10 at 19:43 Zilore Mumba asked Feb 10 at 19:04 Zilore MumbaZilore Mumba 1,5704 gold badges26 silver badges35 bronze badges 3
  • I wish more languages had something like perl's DATA section. Makes self contained example programs that take input from a file so much nicer. – Shawn Commented Feb 11 at 4:20
  • @Shawn Python has StringIO that can read a multiline string as a file. – Chris Charley Commented Feb 11 at 20:19
  • I edited my post with some changes you should probably make. – Chris Charley Commented Feb 12 at 19:52
Add a comment  | 

1 Answer 1

Reset to default 1

I was able to get results from your DATA post.

EDIT: removed the loop of created filehandles, removed import of re and cleaned up the print statement.

#!/usr/bin/perl
use strict;
use warnings;

(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];
    }   
}

for my $i (0..$#hdr) {
    open my $out, '>', "$hdr[$i].txt" or die "open failed: $!";
    for my $j (0..$#area) {
        print $out "$area[$j], $data{$hdr[$i]}[$j]\n";
    }
    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>

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>