Here's one way that may help you get started.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.