It uses a hash of arrays.
#!/bin/perl5 use strict; use warnings; use Data::Dumper; my %data; my $file; while (my $line = <DATA>){ if ($line =~ /([^.]+\.csv)/){ $file = $1; $data{$file} = []; } elsif ($line =~ /(-?[\d.]+)/){ push @{$data{$file}}, $1; } } for my $file (keys %data){ print "file: $file\n"; # open $file print "data:\n"; print "\t$_\n" for @{$data{$file}}; print "\n"; # print data to file } #print Dumper \%data; __DATA__ filename = without012.csv pred = -0.0105 0.0645 0.1989 0.0151 0.0289 0.0400 0.5313 filename = without013.csv pred = -0.0080 0.0663 0.1911 0.0075 0.0288 0.0302 0.5234 filename = without014.csv pred = -0.0023 0.0595 0.2582 0.0323 0.0285 0.0578 0.5807
output:
---------- Capture Output ---------- > "C:\Perl\bin\perl.exe" _new.pl file: without012.csv data: -0.0105 0.0645 0.1989 0.0151 0.0289 0.0400 0.5313 file: without013.csv data: -0.0080 0.0663 0.1911 0.0075 0.0288 0.0302 0.5234 file: without014.csv data: -0.0023 0.0595 0.2582 0.0323 0.0285 0.0578 0.5807 > Terminated with exit code 0.
Hope this helps
In reply to Re: extracting data and saving into seperate files
by wfsp
in thread extracting data and saving into seperate files
by Angharad
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |