Thread Datei einlesen (38 answers)
Opened by Fredl at 2012-03-09 15:50

Linuxer
 2012-03-22 11:36
#156998 #156998
User since
2006-01-27
3891 Artikel
HausmeisterIn

user image
Wenn ich Dich richtig verstehe, suchst Du sowas?

Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#! /usr/bin/perl
use strict;
use warnings;

my @output;
my $count = 0;

# read input data line by line
while ( my $line = <DATA> ) {

    # one block in input data file starts with "Time Step X" and ends with empty line
    if ( $line =~ m{^Time Step \d+$} .. $line =~ m{^\s*$} ) {

        # save data blocks 
        push @output, $line;

        # process data block when end of block
        if ( $line =~ m{^\s*$} ) {

            for my $i ( 1 .. 24 ) {

                # replace number in time step line
                $output[0] =~ s/\d+$/ ++$count/e;

                # print data block to STDOUT
                print @output;

            }

            # reset @output for next data block
            @output = ();
        }
    }
}

__DATA__
Time Step 1
a
b
c
d

Time Step 2
e
f
g
h

Time Step 3
i
j
k
l

Last edited: 2012-03-22 11:37:59 +0100 (CET)
meine Beiträge: I.d.R. alle Angaben ohne Gewähr und auf Linux abgestimmt!
Die Sprache heisst Perl, nicht PERL. - Bitte Crossposts als solche kenntlich machen!

View full thread Datei einlesen