K
kenslaterpa
Marek said:Have no idea, whether this one is an easy task, or not. I have a file with
tab-delimited Data. These Data have two parts, and I want to read in the
Data in two different arrays from a certain /keyword/ on. I made an
exercise, thinking, wow it's working, but later I realized, that everything
was read into the first array :-( My idea of solving this task seems
probably naive, but I have no clue, how to achieve this otherwise.
Is there somebody, who could give me a hint?
thank you all for your patience
marek
#############
#!/usr/bin/perl
use warnings;
use strict;
my (@lines1, @lines2);
while (<DATA>)
{
push @lines1, $_;
if (/keyword/)
{
push @lines2, $_;
}
}
print "here are the first part of lines:\n";
print join ("\n",@lines1);
print "here are the second part of lines:\n";
print join ("\n",@lines2);
__DATA__
37086,4 15445 808 19,5 3156,3
37667 15769,2 817 19,5 3621
37936,5 15929,6 823 19,5 3857,8
38147,5 16058 827 19,5 4042,8
38371,8 16188,6 833 19,5 4247,4
38607,7 16252,7 837 19,5 4359,2
38752,7 16351,6 844 20,5 4523,9
38774,2 16351,6 844 20,5 4526,9
39056,9 16499,8 849 20,5 4740,6
39249,2 16574,6 853 20,5 4854,6
39720,7 16762,9 864 21,5 5148,7
39932 16847,6 872 22 5289,8
40002,6 16852,9 874 22 5307
keyword
17.07.2006 CC mU 58.00
17.07.2006 Fr. Knorr CC mU 60.00
18.07.2006 Unterföhring MUC Link CC mU 45.00
19.07.2006 CC mU 58.00
20.07.2006 MUC Hanauer Hermann/Wacki CC mU 55.00
21.07.2006 Claudio/Rock CC oU 55.00
24.07.2006 CC mU 54.00
24.07.2006 CC mU 50.00
24.07.2006 CC mU 55.00
25.07.2006 CC mU 82.00
26.07.2006 -3.7 Uhr! Königin MUC Wacki Bar oU
55.00
27.07.2006 hin rück Link CC mU 122.00
Based on looking at the script (I haven't run it), I would think your
@lines2 array
would have one element - the keyword line. As your 'if' statement will
only be
true on that line.
if (/keyword/)
{
push @lines2, $_;
}
All lines are being pushed onto @lines1 without any check.
A quick solution is to set a variable when you hit the keyword line:
$foundKeyword = 1 if /keyword/;
Then push onto the appropriate array based on the value of this
variable
($foundKeyword in the above example).
Ken