2013-02-19T11:50:42 Student87Hi,
Wieso benötigt ein Array viel mehr Speicher als ein file auf der Festplatte ?
Quotewenn jede zeile nur aus einem buchstaben besteht und damit bytes gemeint sind und keine umlaute, dann kannst du auch einfach mit seek() zur gewünschten zeile springen.
2013-02-19T11:50:42 Student87Wie kann ich mir aus einem riesigen file mit 250 Mio Zeilen schnell eine Zeile rauspicken?
perl -ne '$. == 13 && print && exit'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#!/usr/bin/perl use strict; use warnings; # 1 -> Zeile 1 # 2 -> Zeile 2 # ... my $position=100; # -> Zeile 100 my $file='test.dat'; my $bytepos=($position-1)*2; # *3 wenn \r\n open(my $fh, '<', $file) or die("ERROR open $file ($!)\n"); seek($fh,$bytepos,0); # vom Anfang der Datei $bytepos Bytes weit gehen. chomp(my $data=<$fh>); close($fh); print "$data\n";
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#!/usr/bin/perl use strict; use warnings; # 1 -> Zeile 1 # 2 -> Zeile 2 # ... my $position=100; # -> Zeile 100 my $file='test.dat'; my $bytepos=($position-1)*2; # *3 wenn \r\n open(my $fh, '<', $file) or die("ERROR open $file ($!)\n"); my $data=''; read($fh,$data,1,$bytepos); close($fh); print "$data\n";
Quoteread FILEHANDLE,SCALAR,LENGTH,OFFSET
read FILEHANDLE,SCALAR,LENGTH
[...]
An OFFSET may be specified to place the read data at some place in the string other
than the beginning. A negative OFFSET specifies placement at that many characters
counting backwards from the end of the string. A positive OFFSET greater than the
length of SCALAR results in the string being padded to the required size with "\0"
bytes before the result of the read is appended.
1
2
3
4
5
6
7
$ perl -wE'
my $buffer = "abcdefghijklmno";
read STDIN, $buffer, 3, 5;
say $buffer;
'
1234567890
abcde123
2013-02-19T13:21:08 Student87Die dritte Liste enthält aber unregelmäßige Bytezahlen pro Zeile.
Quoteeigentlich benötige ich diesen Array 2 x (Der zweite wird modifiziert -> Viele Stellen werden "maskiert")
1 2 3 4
open(IN,"<test_values") or die "$!"; read(IN, my $x, 1, 3); close IN or die "$!"; print"$x\n" #Output: A
2013-02-20T13:16:52 Student87Code (perl): (dl )open(IN,"<test_values") or die "$!";