snippetMinor
Read lines from a file in chunks
Viewed 0 times
filereadchunksfromlines
Problem
I created a function to read lines from an file into chunks.
My hidden agenda in creation this script was in python the yield function in interaction with chunks.
The script works fine, but now i want to know if anyone has improvements?
My hidden agenda in creation this script was in python the yield function in interaction with chunks.
The script works fine, but now i want to know if anyone has improvements?
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
sub read_in_chunks {
my $args = shift;
my $self = {
fd => $args->{fd} || undef,
chunk_size => $args->{chunk_size} || 10,
chunks => [],
};
my $fh = $self->{fd};
return unless defined(my $line=);
while(){
chomp($_);
# maybe the following line could be written nicer :)
($self->{chunk_size} == 0) ? return $self->{chunks} : (push @{$self->{chunks}}, $_);
$self->{chunk_size}--;
}
return $self->{chunks};
}
open my $fh, 'dump.txt' or die $!;
my $opts = {
fd => $fh,
chunk_size => 4
};
while(my $chunk = read_in_chunks($opts)) {
print Dumper($chunk);
# process data
}
close $fh;Solution
Here's my take. I removed the $self structure, as it gives you no advantage, and fixed the problem with missing lines.
#! /usr/bin/perl
use warnings;
use strict;
use Data::Dumper;
sub read_in_chunks {
my %args = @_;
my $fh = $args{fh} or die "No filehandle given.\n";
my $size = $args{chunk_size} || 10;
my @chunk;
while (@chunk )) {
chomp $line;
push @chunk, $line;
}
return @chunk
}
open my $fh, shift or die $!;
my %opts = (
fh => $fh,
chunk_size => 4
);
while (my @chunk = read_in_chunks(%opts)) {
print Dumper(\@chunk);
# ...
}Code Snippets
#! /usr/bin/perl
use warnings;
use strict;
use Data::Dumper;
sub read_in_chunks {
my %args = @_;
my $fh = $args{fh} or die "No filehandle given.\n";
my $size = $args{chunk_size} || 10;
my @chunk;
while (@chunk < $size && defined(my $line = <$fh>)) {
chomp $line;
push @chunk, $line;
}
return @chunk
}
open my $fh, shift or die $!;
my %opts = (
fh => $fh,
chunk_size => 4
);
while (my @chunk = read_in_chunks(%opts)) {
print Dumper(\@chunk);
# ...
}Context
StackExchange Code Review Q#163466, answer score: 3
Revisions (0)
No revisions yet.