in reply to Re: rectangularizing input to become array
in thread rectangularizing input to become array
I'm making every effort to quote haukex fairly, but I will re-order for thematic and write-up reasons. I threw the tab character in to be a possible problem. I think I deal with it with:
$input=~ s/\t/ /g;I'm also trying to make the write-up as austere as it can be in terms of using vertical space, so I will continue in readmore tags. I think I get more eyes if people don't have to scroll down to continue finding good content, and the thread might read more about the solutions as opposed to the problem. I haven't even gotten to the third one yet.
I'm reminded what someone told me the first time I got on the U-bahn in Berlin with my bike: "Das hast Du voellig verkehrt getan." I figured out what signs he was pointed at and never made the same mistake. Were it only one and done with perl....
So there's the base directory of the script. I wouldn't want output there. I've now put a module in the lib folder that presumably would be done before release. It is certainly not in such a state now. Given that we need a "hard" output file, would you rather put such a thing on our one and only subdirectory or split input and output into separate directories? Won't that complicate diff'ing?
you've included a link to GitHub, which might go down sometime in the futureAnd they may change over time. For example, I added a module to lib/ which brought the size of the zip file from 1.1 to 3.1 k. Should I go update that on the original post?
For this subthread, I would like to stick to the SSCCE input you offered.
in your code tags you've included the command-line invocations that I'd have to trimI tend to think that it provides context; where I might describe a situation completely "verkehrt," the computer commands inform the sleuths who can divine what I am actually asking my computer to do. Sometimes I can get too cute. Sometimes, I can't read my own input or output. We have to be somewhat ecumenical about the starts of scripts. Might pre tags work here? I'll put pre tags on the output and invocation line, and leave everything inside code tags be an executable.
$ ./3.rm.pl "abcdef", "abcdefg", "abcde", " bcdefgh", " bcd " "abcdef", "abcdef", "abcde ", " bcdef" inside first anonymous block Can't use string ("abcdef") as an ARRAY ref while "strict refs" in use at ./3.rm.pl line 59. $ cat 3.rm.pl
#!/usr/bin/perl -w use 5.011; use Data::Dump; my $input = <<'(END INPUT)'; abcdef abcdefg abcde bcdefgh bcd (END INPUT) $input=~ s/\t/ /g; my @lines = split /\n/, $input; dd \@lines; my $out = make_rectangular( \@lines, 4, 6 ); dd $out; use Test::More; { say "inside first anonymous block"; my $subset = getsubset( $out, "R1" ); is_deeply $subset, [ [ 'a' .. 'f' ] ]; say "exiting first anonymous block"; } sub make_rectangular { my ( $lines, $maxrows, $maxlength ) = @_; my @out; my $rowcount=1; for my $line (@$lines) { my $trimmed = substr $line, 0, $maxlength; push @out, sprintf "%-*s", $maxlength, $trimmed; last if ++$rowcount>$maxrows; } return \@out; } sub rangeparse { use Carp; local $_ = shift; my @o; # [ row1,col1, row2,col2 ] (-1 = last row/col) if ( @o = /\AR([0-9]+|n)C([0-9]+|n):R([0-9]+|n)C([0-9]+|n)\z/ ) { } elsif (/\AR([0-9]+|n):R([0-9]+|n)\z/) { @o = ( $1, 1, $2, -1 ) } elsif (/\AC([0-9]+|n):C([0-9]+|n)\z/) { @o = ( 1, $1, -1, $2 ) } elsif (/\AR([0-9]+|n)C([0-9]+|n)\z/) { @o = ( $1, $2, $1, $2 ) } elsif (/\AR([0-9]+|n)\z/) { @o = ( $1, 1, $1, -1 ) } elsif (/\AC([0-9]+|n)\z/) { @o = ( 1, $1, -1, $1 ) } else { croak "failed to parse '$_'" } $_ eq 'n' and $_ = -1 for @o; return \@o; } sub getsubset { use Carp; my ( $data, $range ) = @_; my $cols = @{ $$data[0] }; @$_ == $cols or croak "data not rectangular" for @$data; $range = rangeparse($range) unless ref $range eq 'ARRAY'; @$range == 4 or croak "bad size of range"; my @max = ( 0 + @$data, $cols ) x 2; for my $i ( 0 .. 3 ) { $$range[$i] = $max[$i] if $$range[$i] < 0; croak "index $i out of range" if $$range[$i] < 1 || $$range[$i] > $max[$i]; } croak "bad rows $$range[0]-$$range[2]" if $$range[0] > $$range[2]; croak "bad cols $$range[1]-$$range[3]" if $$range[1] > $$range[3]; my @cis = $$range[1] - 1 .. $$range[3] - 1; return [ map { sub { \@_ } ->( @{ $$data[$_] }[@cis] ) } $$range[0] - 1 .. $$range[2] - 1 ]; } __END__
The ultimate two routines are from Selecting Ranges of 2-Dimensional Data, and work fine with other data.
What I seek to do is pass the first test...then others....
Vielen Dank und Schoenen Gruss aus Amiland,
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: rectangularizing input to become array
by haukex (Archbishop) on Feb 28, 2019 at 23:08 UTC | |
by Aldebaran (Curate) on Mar 02, 2019 at 23:12 UTC | |
by haukex (Archbishop) on Mar 03, 2019 at 11:24 UTC | |
by Aldebaran (Curate) on Mar 01, 2019 at 00:27 UTC |