diff options
| author | drbaggy <js5@sanger.ac.uk> | 2021-04-28 09:38:44 +0100 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2021-04-28 09:38:44 +0100 |
| commit | 4fb433346a210a16bc92a7f1f3c7749d69eb502b (patch) | |
| tree | 47488481ffe0b24c18bfee10b84c1a670c205c57 | |
| parent | eed3560d163299335d7afcd3ed2532d00143db09 (diff) | |
| download | perlweeklychallenge-club-4fb433346a210a16bc92a7f1f3c7749d69eb502b.tar.gz perlweeklychallenge-club-4fb433346a210a16bc92a7f1f3c7749d69eb502b.tar.bz2 perlweeklychallenge-club-4fb433346a210a16bc92a7f1f3c7749d69eb502b.zip | |
Tidied up seek method
| -rw-r--r-- | challenge-110/james-smith/README.md | 15 | ||||
| -rw-r--r-- | challenge-110/james-smith/perl/ch-2.pl | 17 |
2 files changed, 17 insertions, 15 deletions
diff --git a/challenge-110/james-smith/README.md b/challenge-110/james-smith/README.md index 433fbf84f7..22b9d2b788 100644 --- a/challenge-110/james-smith/README.md +++ b/challenge-110/james-smith/README.md @@ -172,8 +172,8 @@ sub transpose_seek { ## Loop through the file and get the start/end position of each line, ## and the first $BYTES characters of each line... - push ( @pos, [$prev+$BYTES,tell $fh,substr $_,0,$BYTES]) && - ( $prev=tell $fh ) while <$fh>; + ( push @pos, [ $prev+$BYTES, tell $fh, substr $_, 0, $BYTES] ) && + ( $prev = tell $fh ) while <$fh>; ## While we still have "columns" loop through each row and grab the first ## entry and output results. @@ -181,16 +181,17 @@ sub transpose_seek { while( $pos[0][0] < $pos[0][1] || length $pos[0][2] ) { my @line; foreach(@pos) { + ## Grab extra data for the row if we have got an incomplete + ## field {missing a "," and still data to read} while( $_->[2] !~ m{,} && $_->[0] < $_->[1] ) { seek $fh, $_->[0], 0; - read $fh, - $_->[2], ## "Buffer" + read $fh, $_->[2], ## "Buffer" $_->[1]-$_->[0] > $BYTES ? $BYTES : $_->[1]-$_->[0], - length $_->[2]; ## Length of "Buffer" so text gets added to end + length $_->[2]; ## Length of "Buffer" so text gets + ## added to end $_->[0]+=$BYTES; } - $_->[2] =~ s{^([^,\r\n]+)[,\r\n]*}{}; - push @line, $1; + push @line, $_->[2] =~ s{^([^,\r\n]+)[,\r\n]*}{}; } say {$ofh} join q(,), @line; } diff --git a/challenge-110/james-smith/perl/ch-2.pl b/challenge-110/james-smith/perl/ch-2.pl index 4afa11e49c..c71cbca113 100644 --- a/challenge-110/james-smith/perl/ch-2.pl +++ b/challenge-110/james-smith/perl/ch-2.pl @@ -78,12 +78,12 @@ sub transpose_seek { open my $fh, '<', $_[0]; open my $ofh, '>', $_[1]; - + ## Loop through the file and get the start/end position of each line, ## and the first $BYTES characters of each line... - push ( @pos, [$prev+$BYTES,tell $fh,substr $_,0,$BYTES]) && - ( $prev=tell $fh ) while <$fh>; + ( push @pos, [ $prev+$BYTES, tell $fh, substr $_, 0, $BYTES] ) && + ( $prev = tell $fh ) while <$fh>; ## While we still have "columns" loop through each row and grab the first ## entry and output results. @@ -91,16 +91,17 @@ sub transpose_seek { while( $pos[0][0] < $pos[0][1] || length $pos[0][2] ) { my @line; foreach(@pos) { + ## Grab extra data for the row if we have got an incomplete + ## field {missing a "," and still data to read} while( $_->[2] !~ m{,} && $_->[0] < $_->[1] ) { seek $fh, $_->[0], 0; - read $fh, - $_->[2], ## "Buffer" + read $fh, $_->[2], ## "Buffer" $_->[1]-$_->[0] > $BYTES ? $BYTES : $_->[1]-$_->[0], - length $_->[2]; ## Length of "Buffer" so text gets added to end + length $_->[2]; ## Length of "Buffer" so text gets + ## added to end $_->[0]+=$BYTES; } - $_->[2] =~ s{^([^,\r\n]+)[,\r\n]*}{}; - push @line, $1; + push @line, $_->[2] =~ s{^([^,\r\n]+)[,\r\n]*}{}; } say {$ofh} join q(,), @line; } |
