got to be a better way
User:
afbach
Date: 2/16/2012 6:12 pm
Date: 2/16/2012 6:12 pm
Views: 731
Rating: 0
Rating: 0
A 'C' friend wrote this to munge pipe delim sql data into text tables
#!/usr/bin/perl
die "usage: fmtdump [header1 header2 ...]\n" if ($#ARGV < 0);
open(INFILE, "<$ARGV[0]");
while () {
chomp; @t = split(/\|/);
for $i (0 .. $#t) {
$c[$i] = length($ARGV[$i+1]) if (! $c[$i]);
$t[$i] =~ s/^\s+|\s+$//g;
$c[$i] = length($t[$i]) > $c[$i] ? length($t[$i]) : $c[$i];
}
push @s, [ @t ];
}
for $i (0 .. $#c) {
printf("%-$c[$i]s ", $ARGV[$i+1]);
$l .= "-" x ($c[$i] + 1);
}
print "\n$l\n";
for $i (0 .. $#s) {
for $j (0 .. $#{$s[$i]}) {
printf("%-$c[$j]s ", $s[$i][$j]);
}
print "\n";
}
print "\n";
The idea being you put the headers on the command line and it handles
the spacing and the 2nd line of dashes. It's straightforward but ...
a
--
a
Andy Bach,
afbach@gmail.com
608 658-1890 cell
608 261-5738 wk
#!/usr/bin/perl
die "usage: fmtdump [header1 header2 ...]\n" if ($#ARGV < 0);
open(INFILE, "<$ARGV[0]");
while () {
chomp; @t = split(/\|/);
for $i (0 .. $#t) {
$c[$i] = length($ARGV[$i+1]) if (! $c[$i]);
$t[$i] =~ s/^\s+|\s+$//g;
$c[$i] = length($t[$i]) > $c[$i] ? length($t[$i]) : $c[$i];
}
push @s, [ @t ];
}
for $i (0 .. $#c) {
printf("%-$c[$i]s ", $ARGV[$i+1]);
$l .= "-" x ($c[$i] + 1);
}
print "\n$l\n";
for $i (0 .. $#s) {
for $j (0 .. $#{$s[$i]}) {
printf("%-$c[$j]s ", $s[$i][$j]);
}
print "\n";
}
print "\n";
The idea being you put the headers on the command line and it handles
the spacing and the 2nd line of dashes. It's straightforward but ...
a
--
a
Andy Bach,
afbach@gmail.com
608 658-1890 cell
608 261-5738 wk
Re: got to be a better way
User:
jt
Date: 2/16/2012 6:22 pm
Date: 2/16/2012 6:22 pm
Views: 0
Rating: 0
Rating: 0
The Text::CSV_XS module on CPAN can munge pipe delimited data for you, and then give you an iterator to walk to read the data out row by row. I'd go that route.
On Feb 16, 2012, at 6:12 PM, <afbach@gmail.com> wrote:
afbach wrote:
A 'C' friend wrote this to munge pipe delim sql data into text tables
#!/usr/bin/perl
die "usage: fmtdump [header1 header2 ...]\n" if ($#ARGV < 0);
open(INFILE, "<$ARGV[0]");
while () {
chomp; @t = split(/\|/);
for $i (0 .. $#t) {
$c[$i] = length($ARGV[$i+1]) if (! $c[$i]);
$t[$i] =~ s/^\s+|\s+$//g;
$c[$i] = length($t[$i]) > $c[$i] ? length($t[$i]) : $c[$i];
}
push @s, [ @t ];
}
for $i (0 .. $#c) {
printf("%-$c[$i]s ", $ARGV[$i+1]);
$l .= "-" x ($c[$i] + 1);
}
print "\n$l\n";
for $i (0 .. $#s) {
for $j (0 .. $#{$s[$i]}) {
printf("%-$c[$j]s ", $s[$i][$j]);
}
print "\n";
}
print "\n";
The idea being you put the headers on the command line and it handles
the spacing and the 2nd line of dashes. It's straightforward but ...
a
--
a
Andy Bach,
afbach@gmail.com
608 658-1890 cell
608 261-5738 wk
Madison Area Perl Mongers - MadMongers
http://www.madmongers.org
Re: got to be a better way
User:
mcholste
Date: 2/17/2012 12:28 pm
Date: 2/17/2012 12:28 pm
Views: 148
Rating: 0
Rating: 0
Dear Lord, that is some coding horror. *THIS* is the kind of stuff
that gives Perl such a bad name. If there is more punctuation than
alphanumeric chars in your code, you're doing it wrong.
On Thu, Feb 16, 2012 at 6:22 PM, wrote:
> jt wrote:
>
> The Text::CSV_XS module on CPAN can munge pipe delimited data for you, and
> then give you an iterator to walk to read the data out row by row. I'd go
> that route.
>
>
> On Feb 16, 2012, at 6:12 PM, wrote:
>
> afbach wrote:
>
> A 'C' friend wrote this to munge pipe delim sql data into text tables
> #!/usr/bin/perl
>
> die "usage: fmtdump [header1 header2 ...]\n" if ($#ARGV < 0);
>
> open(INFILE, "<$ARGV[0]");
> while () {
> chomp; @t = split(/\|/);
> for $i (0 .. $#t) {
> $c[$i] = length($ARGV[$i+1]) if (! $c[$i]);
> $t[$i] =~ s/^\s+|\s+$//g;
> $c[$i] = length($t[$i]) > $c[$i] ? length($t[$i]) : $c[$i];
> }
> push @s, [ @t ];
> }
>
> for $i (0 .. $#c) {
> printf("%-$c[$i]s ", $ARGV[$i+1]);
> $l .= "-" x ($c[$i] + 1);
> }
> print "\n$l\n";
>
> for $i (0 .. $#s) {
> for $j (0 .. $#{$s[$i]}) {
> printf("%-$c[$j]s ", $s[$i][$j]);
> }
> print "\n";
> }
> print "\n";
>
> The idea being you put the headers on the command line and it handles
> the spacing and the 2nd line of dashes. It's straightforward but ...
>
> a
> --
>
> a
>
> Andy Bach,
> afbach@gmail.com
> 608 658-1890 cell
> 608 261-5738 wk
>
> View Online
>
>
>
> Madison Area Perl Mongers - MadMongers
> http://www.madmongers.org
>
>
> View Online
>
>
>
> Madison Area Perl Mongers - MadMongers
> http://www.madmongers.org
that gives Perl such a bad name. If there is more punctuation than
alphanumeric chars in your code, you're doing it wrong.
On Thu, Feb 16, 2012 at 6:22 PM, wrote:
> jt wrote:
>
> The Text::CSV_XS module on CPAN can munge pipe delimited data for you, and
> then give you an iterator to walk to read the data out row by row. I'd go
> that route.
>
>
> On Feb 16, 2012, at 6:12 PM, wrote:
>
> afbach wrote:
>
> A 'C' friend wrote this to munge pipe delim sql data into text tables
> #!/usr/bin/perl
>
> die "usage: fmtdump [header1 header2 ...]\n" if ($#ARGV < 0);
>
> open(INFILE, "<$ARGV[0]");
> while () {
> chomp; @t = split(/\|/);
> for $i (0 .. $#t) {
> $c[$i] = length($ARGV[$i+1]) if (! $c[$i]);
> $t[$i] =~ s/^\s+|\s+$//g;
> $c[$i] = length($t[$i]) > $c[$i] ? length($t[$i]) : $c[$i];
> }
> push @s, [ @t ];
> }
>
> for $i (0 .. $#c) {
> printf("%-$c[$i]s ", $ARGV[$i+1]);
> $l .= "-" x ($c[$i] + 1);
> }
> print "\n$l\n";
>
> for $i (0 .. $#s) {
> for $j (0 .. $#{$s[$i]}) {
> printf("%-$c[$j]s ", $s[$i][$j]);
> }
> print "\n";
> }
> print "\n";
>
> The idea being you put the headers on the command line and it handles
> the spacing and the 2nd line of dashes. It's straightforward but ...
>
> a
> --
>
> a
>
> Andy Bach,
> afbach@gmail.com
> 608 658-1890 cell
> 608 261-5738 wk
>
> View Online
>
>
>
> Madison Area Perl Mongers - MadMongers
> http://www.madmongers.org
>
>
> View Online
>
>
>
> Madison Area Perl Mongers - MadMongers
> http://www.madmongers.org
Re: got to be a better way
User:
esayward
Date: 2/17/2012 1:20 pm
Date: 2/17/2012 1:20 pm
Views: 0
Rating: 0
Rating: 0
this is painfull to watch!
----- Original Message -----From: mcholste@gmail.comTo: admin@wisbin.comSent: Friday, February 17, 2012 12:28 PMSubject: [MadTalk] Re: got to be a better waymcholste wrote:
Dear Lord, that is some coding horror. *THIS* is the kind of stuff
that gives Perl such a bad name. If there is more punctuation than
alphanumeric chars in your code, you're doing it wrong.
On Thu, Feb 16, 2012 at 6:22 PM, wrote:
> jt wrote:
>
> The Text::CSV_XS module on CPAN can munge pipe delimited data for you, and
> then give you an iterator to walk to read the data out row by row. I'd go
> that route.
>
>
> On Feb 16, 2012, at 6:12 PM, wrote:
>
> afbach wrote:
>
> A 'C' friend wrote this to munge pipe delim sql data into text tables
> #!/usr/bin/perl
>
> die "usage: fmtdump [header1 header2 ...]\n" if ($#ARGV < 0);
>
> open(INFILE, "<$ARGV[0]");
> while () {
> chomp; @t = split(/\|/);
> for $i (0 .. $#t) {
> $c[$i] = length($ARGV[$i+1]) if (! $c[$i]);
> $t[$i] =~ s/^\s+|\s+$//g;
> $c[$i] = length($t[$i]) > $c[$i] ? length($t[$i]) : $c[$i];
> }
> push @s, [ @t ];
> }
>
> for $i (0 .. $#c) {
> printf("%-$c[$i]s ", $ARGV[$i+1]);
> $l .= "-" x ($c[$i] + 1);
> }
> print "\n$l\n";
>
> for $i (0 .. $#s) {
> for $j (0 .. $#{$s[$i]}) {
> printf("%-$c[$j]s ", $s[$i][$j]);
> }
> print "\n";
> }
> print "\n";
>
> The idea being you put the headers on the command line and it handles
> the spacing and the 2nd line of dashes. It's straightforward but ...
>
> a
> --
>
> a
>
> Andy Bach,
> afbach@gmail.com
> 608 658-1890 cell
> 608 261-5738 wk
>
> View Online
>
>
>
> Madison Area Perl Mongers - MadMongers
> http://www.madmongers.org
>
>
> View Online
>
>
>
> Madison Area Perl Mongers - MadMongers
> http://www.madmongers.org
Madison Area Perl Mongers - MadMongers
http://www.madmongers.org
Re: got to be a better way
User:
afbach
Date: 2/17/2012 1:53 pm
Date: 2/17/2012 1:53 pm
Views: 0
Rating: 0
Rating: 0
On Fri, Feb 17, 2012 at 12:28 PM, wrote:
> Dear Lord, that is some coding horror.
Well, again - it's from a 'C' programmer, a sys admin in a small shop
who's just learning Perl, hence the one char names. I can't even
guess what they're supposed to be the initial of but I thought the
plan was good - not that aren't better ways to do it (modules are not
something we're supposed to install on the production server). Still
finding the longest word in each column so your headers can be aligned
w/ the left justified text (probably this was originally ... er,
perhaps still is, printed to a serial port attached printer) was
clever. I couldn't come up w/ a better plan, though I could make it
strict safe and use real words, along with a couple better loops.
Still, had to use for my $i ( 0 .. $# ) to handle the length
test and get the printf formating to work.
--
a
Andy Bach,
afbach@gmail.com
608 658-1890 cell
608 261-5738 wk
> Dear Lord, that is some coding horror.
Well, again - it's from a 'C' programmer, a sys admin in a small shop
who's just learning Perl, hence the one char names. I can't even
guess what they're supposed to be the initial of but I thought the
plan was good - not that aren't better ways to do it (modules are not
something we're supposed to install on the production server). Still
finding the longest word in each column so your headers can be aligned
w/ the left justified text (probably this was originally ... er,
perhaps still is, printed to a serial port attached printer) was
clever. I couldn't come up w/ a better plan, though I could make it
strict safe and use real words, along with a couple better loops.
Still, had to use for my $i ( 0 .. $# ) to handle the length
test and get the printf formating to work.
--
a
Andy Bach,
afbach@gmail.com
608 658-1890 cell
608 261-5738 wk
Re: got to be a better way
User:
miner
Date: 2/17/2012 2:28 pm
Date: 2/17/2012 2:28 pm
Views: 0
Rating: 0
Rating: 0
No, it's what gives C a bad name.
That is not Perl, that is C, written in Perl. And, yes, the Perl sigils make it look even worse than the C that it is.
And it's full of poor Perl constructs and traps waiting to catch people. My guess is it's written by someone who learned C back when storage/memory was really expensive, and who taught themselves Perl and just figured out things that "work." ( "while ()" for example. )
jon
On 2/17/12 12:28 PM, mcholste@gmail.com wrote:
That is not Perl, that is C, written in Perl. And, yes, the Perl sigils make it look even worse than the C that it is.
And it's full of poor Perl constructs and traps waiting to catch people. My guess is it's written by someone who learned C back when storage/memory was really expensive, and who taught themselves Perl and just figured out things that "work." ( "while ()" for example. )
jon
On 2/17/12 12:28 PM, mcholste@gmail.com wrote:
mcholste wrote:
Dear Lord, that is some coding horror. *THIS* is the kind of stuff
that gives Perl such a bad name. If there is more punctuation than
alphanumeric chars in your code, you're doing it wrong.
On Thu, Feb 16, 2012 at 6:22 PM, wrote:
> jt wrote:
>
> The Text::CSV_XS module on CPAN can munge pipe delimited data for you, and
> then give you an iterator to walk to read the data out row by row. I'd go
> that route.
>
>
> On Feb 16, 2012, at 6:12 PM, wrote:
>
> afbach wrote:
>
> A 'C' friend wrote this to munge pipe delim sql data into text tables
> #!/usr/bin/perl
>
> die "usage: fmtdump [header1 header2 ...]\n" if ($#ARGV < 0);
>
> open(INFILE, "<$ARGV[0]");
> while () {
> chomp; @t = split(/\|/);
> for $i (0 .. $#t) {
> $c[$i] = length($ARGV[$i+1]) if (! $c[$i]);
> $t[$i] =~ s/^\s+|\s+$//g;
> $c[$i] = length($t[$i]) > $c[$i] ? length($t[$i]) : $c[$i];
> }
> push @s, [ @t ];
> }
>
> for $i (0 .. $#c) {
> printf("%-$c[$i]s ", $ARGV[$i+1]);
> $l .= "-" x ($c[$i] + 1);
> }
> print "\n$l\n";
>
> for $i (0 .. $#s) {
> for $j (0 .. $#{$s[$i]}) {
> printf("%-$c[$j]s ", $s[$i][$j]);
> }
> print "\n";
> }
> print "\n";
>
> The idea being you put the headers on the command line and it handles
> the spacing and the 2nd line of dashes. It's straightforward but ...
>
> a
> --
>
> a
>
> Andy Bach,
> afbach@gmail.com
> 608 658-1890 cell
> 608 261-5738 wk
>
> View Online
>
>
>
> Madison Area Perl Mongers - MadMongers
> http://www.madmongers.org
>
>
> View Online
>
>
>
> Madison Area Perl Mongers - MadMongers
> http://www.madmongers.org
Madison Area Perl Mongers - MadMongers
http://www.madmongers.org
-- .Jonathan J. Miner----------------------------------------------------. | jon@jjminer.org | photos - http://photos.jjminer.org/ | | | R.A.W. #1629 - http://www.reggaeambassadors.org | | | LOCS Webmaster - http://www.locs-buffett.org | | jabber/gchat: camrycurbhopper@gmail.com | `---------------------------------------------------------------------' "We don't have a town drunk... We all take turns!"
Re: got to be a better way
User:
david-delikat
Date: 2/17/2012 3:26 pm
Date: 2/17/2012 3:26 pm
Views: 0
Rating: 0
Rating: 0
my @headers = @ARGV;
open my($fh), '<', $filename or die' failed to open file',$filename;
my @colWidths = map { length $_ } @headers;
sub maxSizes {
my($target,$new) = @_;
for( 0 .. $#$target ) {
$target->[$_] = ( $target->[$_] < $new->[$_] ? $new->[$_] : $target->[$_] ) + 1;
}
}
my @data = ( [ @headers ] );
while( <$fh> ) {
my $row = [ split '|' ];
my @sizes = map { length $_ } @$row;
maxSizes(\@colWidths,\@sizes);
push @data, $row;
}
for my $row ( @data ) {
for ( 0 .. $#$row ) {
printf "$-*s", $colWidths[$_], $row->[$_];
}
print "\n";
}
note that this is entirely untested.
I just threw it together in a few short
coding frenzies…
also note that it could get even more 'Perl-y' if the arrays
were turned into hashes but I left it this way
to keep the memory usage down.
-dav
On Feb 17, 2012, at 12:28 PM, <mcholste@gmail.com> <mcholste@gmail.com> wrote:
mcholste wrote:
Dear Lord, that is some coding horror. *THIS* is the kind of stuff
that gives Perl such a bad name. If there is more punctuation than
alphanumeric chars in your code, you're doing it wrong.
On Thu, Feb 16, 2012 at 6:22 PM, wrote:
> jt wrote:
>
> The Text::CSV_XS module on CPAN can munge pipe delimited data for you, and
> then give you an iterator to walk to read the data out row by row. I'd go
> that route.
>
>
> On Feb 16, 2012, at 6:12 PM, wrote:
>
> afbach wrote:
>
> A 'C' friend wrote this to munge pipe delim sql data into text tables
> #!/usr/bin/perl
>
> die "usage: fmtdump [header1 header2 ...]\n" if ($#ARGV < 0);
>
> open(INFILE, "<$ARGV[0]");
> while () {
> chomp; @t = split(/\|/);
> for $i (0 .. $#t) {
> $c[$i] = length($ARGV[$i+1]) if (! $c[$i]);
> $t[$i] =~ s/^\s+|\s+$//g;
> $c[$i] = length($t[$i]) > $c[$i] ? length($t[$i]) : $c[$i];
> }
> push @s, [ @t ];
> }
>
> for $i (0 .. $#c) {
> printf("%-$c[$i]s ", $ARGV[$i+1]);
> $l .= "-" x ($c[$i] + 1);
> }
> print "\n$l\n";
>
> for $i (0 .. $#s) {
> for $j (0 .. $#{$s[$i]}) {
> printf("%-$c[$j]s ", $s[$i][$j]);
> }
> print "\n";
> }
> print "\n";
>
> The idea being you put the headers on the command line and it handles
> the spacing and the 2nd line of dashes. It's straightforward but ...
>
> a
> --
>
> a
>
> Andy Bach,
> afbach@gmail.com
> 608 658-1890 cell
> 608 261-5738 wk
>
> View Online
>
>
>
> Madison Area Perl Mongers - MadMongers
> http://www.madmongers.org
>
>
> View Online
>
>
>
> Madison Area Perl Mongers - MadMongers
> http://www.madmongers.org
Madison Area Perl Mongers - MadMongers
http://www.madmongers.org
Re: got to be a better way
User:
mcholste
Date: 2/17/2012 4:18 pm
Date: 2/17/2012 4:18 pm
Views: 109
Rating: 0
Rating: 0
Just throw Acme::Bleach on it and you're done!
On Friday, February 17, 2012, <david-delikat@usa.net> wrote:
> david-delikat wrote:
>
> my $filename = shift @ARGV;
> my @headers = @ARGV;
> open my($fh), '<', $filename or die' failed to open file',$filename;
> my @colWidths = map { length $_ } @headers;
> sub maxSizes {
> my($target,$new) = @_;
> for( 0 .. $#$target ) {
> $target->[$_] = ( $target->[$_] < $new->[$_] ? $new->[$_] : $target->[$_] ) + 1;
> }
> }
> my @data = ( [ @headers ] );
> while( <$fh> ) {
> my $row = [ split '|' ];
> my @sizes = map { length $_ } @$row;
> maxSizes(\@colWidths,\@sizes);
> push @data, $row;
> }
> for my $row ( @data ) {
> for ( 0 .. $#$row ) {
> printf "$-*s", $colWidths[$_], $row->[$_];
> }
> print "\n";
> }
> note that this is entirely untested.
> I just threw it together in a few short
> coding frenzies…
> also note that it could get even more 'Perl-y' if the arrays
> were turned into hashes but I left it this way
> to keep the memory usage down.
> -dav
>
> On Feb 17, 2012, at 12:28 PM, <mcholste@gmail.com> <mcholste@gmail.com> wrote:
>
> mcholste wrote:
>
> Dear Lord, that is some coding horror. *THIS* is the kind of stuff
> that gives Perl such a bad name. If there is more punctuation than
> alphanumeric chars in your code, you're doing it wrong.
>
> On Thu, Feb 16, 2012 at 6:22 PM, wrote:
>> jt wrote:
>>
>> The Text::CSV_XS module on CPAN can munge pipe delimited data for you, and
>> then give you an iterator to walk to read the data out row by row. I'd go
>> that route.
>>
>>
>> On Feb 16, 2012, at 6:12 PM, wrote:
>>
>> afbach wrote:
>>
>> A 'C' friend wrote this to munge pipe delim sql data into text tables
>> #!/usr/bin/perl
>>
>> die "usage: fmtdump [header1 header2 ...]\n" if ($#ARGV < 0);
>>
>> open(INFILE, "<$ARGV[0]");
>> while () {
>> chomp; @t = split(/\|/);
>> for $i (0 .. $#t) {
>> $c[$i] = length($ARGV[$i+1]) if (! $c[$i]);
>> $t[$i] =~ s/^\s+|\s+$//g;
>> $c[$i] = length($t[$i]) > $c[$i] ? length($t[$i]) : $c[$i];
>> }
>> push @s, [ @t ];
>> }
>>
>> for $i (0 .. $#c) {
>> printf("%-$c[$i]s ", $ARGV[$i+1]);
>> $l .= "-" x ($c[$i] + 1);
>> }
>> print "\n$l\n";
>>
>> for $i (0 .. $#s) {
>> for $j (0 .. $#{$s[$i]}) {
>> printf("%-$c[$j]s ", $s[$i][$j]);
>> }
>> print "\n";
>> }
>> print "\n";
>>
>> The idea being you put the headers on the command line and it handles
>> the spacing and the 2nd line of dashes. It's straightforward but ...
>>
>> a
>> --
>>
>> a
>>
>> Andy Bach,
>> afbach@gmail.com
>> 608 658-1890 cell
>> 608 261-5738 wk
>>
>> View Online
>>
>>
>>
>> Madison Area Perl Mongers - MadMongers
>> http://www.madmongers.org
>>
>>
>> View Online
>>
>>
>>
>> Madison Area Perl Mongers - MadMongers
>> http://www.madmongers.org
>
> View Online
>
> Madison Area Perl Mongers - MadMongers
> http://www.madmongers.org
>
> View Online
>
> Madison Area Perl Mongers - MadMongers
> http://www.madmongers.org
>
On Friday, February 17, 2012, <david-delikat@usa.net> wrote:
> david-delikat wrote:
>
> my $filename = shift @ARGV;
> my @headers = @ARGV;
> open my($fh), '<', $filename or die' failed to open file',$filename;
> my @colWidths = map { length $_ } @headers;
> sub maxSizes {
> my($target,$new) = @_;
> for( 0 .. $#$target ) {
> $target->[$_] = ( $target->[$_] < $new->[$_] ? $new->[$_] : $target->[$_] ) + 1;
> }
> }
> my @data = ( [ @headers ] );
> while( <$fh> ) {
> my $row = [ split '|' ];
> my @sizes = map { length $_ } @$row;
> maxSizes(\@colWidths,\@sizes);
> push @data, $row;
> }
> for my $row ( @data ) {
> for ( 0 .. $#$row ) {
> printf "$-*s", $colWidths[$_], $row->[$_];
> }
> print "\n";
> }
> note that this is entirely untested.
> I just threw it together in a few short
> coding frenzies…
> also note that it could get even more 'Perl-y' if the arrays
> were turned into hashes but I left it this way
> to keep the memory usage down.
> -dav
>
> On Feb 17, 2012, at 12:28 PM, <mcholste@gmail.com> <mcholste@gmail.com> wrote:
>
> mcholste wrote:
>
> Dear Lord, that is some coding horror. *THIS* is the kind of stuff
> that gives Perl such a bad name. If there is more punctuation than
> alphanumeric chars in your code, you're doing it wrong.
>
> On Thu, Feb 16, 2012 at 6:22 PM, wrote:
>> jt wrote:
>>
>> The Text::CSV_XS module on CPAN can munge pipe delimited data for you, and
>> then give you an iterator to walk to read the data out row by row. I'd go
>> that route.
>>
>>
>> On Feb 16, 2012, at 6:12 PM, wrote:
>>
>> afbach wrote:
>>
>> A 'C' friend wrote this to munge pipe delim sql data into text tables
>> #!/usr/bin/perl
>>
>> die "usage: fmtdump [header1 header2 ...]\n" if ($#ARGV < 0);
>>
>> open(INFILE, "<$ARGV[0]");
>> while () {
>> chomp; @t = split(/\|/);
>> for $i (0 .. $#t) {
>> $c[$i] = length($ARGV[$i+1]) if (! $c[$i]);
>> $t[$i] =~ s/^\s+|\s+$//g;
>> $c[$i] = length($t[$i]) > $c[$i] ? length($t[$i]) : $c[$i];
>> }
>> push @s, [ @t ];
>> }
>>
>> for $i (0 .. $#c) {
>> printf("%-$c[$i]s ", $ARGV[$i+1]);
>> $l .= "-" x ($c[$i] + 1);
>> }
>> print "\n$l\n";
>>
>> for $i (0 .. $#s) {
>> for $j (0 .. $#{$s[$i]}) {
>> printf("%-$c[$j]s ", $s[$i][$j]);
>> }
>> print "\n";
>> }
>> print "\n";
>>
>> The idea being you put the headers on the command line and it handles
>> the spacing and the 2nd line of dashes. It's straightforward but ...
>>
>> a
>> --
>>
>> a
>>
>> Andy Bach,
>> afbach@gmail.com
>> 608 658-1890 cell
>> 608 261-5738 wk
>>
>> View Online
>>
>>
>>
>> Madison Area Perl Mongers - MadMongers
>> http://www.madmongers.org
>>
>>
>> View Online
>>
>>
>>
>> Madison Area Perl Mongers - MadMongers
>> http://www.madmongers.org
>
> View Online
>
> Madison Area Perl Mongers - MadMongers
> http://www.madmongers.org
>
> View Online
>
> Madison Area Perl Mongers - MadMongers
> http://www.madmongers.org
>
Re: got to be a better way
User:
david-delikat
Date: 2/17/2012 4:31 pm
Date: 2/17/2012 4:31 pm
Views: 0
Rating: 0
Rating: 0
On Feb 17, 2012, at 4:18 PM, <mcholste@gmail.com> wrote:
mcholste wrote:
Just throw Acme::Bleach on it and you're done!
On Friday, February 17, 2012, <david-delikat@usa.net> wrote:
> david-delikat wrote:
>
> my $filename = shift @ARGV;
> my @headers = @ARGV;
> open my($fh), '<', $filename or die' failed to open file',$filename;
> my @colWidths = map { length $_ } @headers;
> sub maxSizes {
> my($target,$new) = @_;
> for( 0 .. $#$target ) {
> $target->[$_] = ( $target->[$_] < $new->[$_] ? $new->[$_] : $target->[$_] ) + 1;
> }
> }
> my @data = ( [ @headers ] );
> while( <$fh> ) {
> my $row = [ split '|' ];
> my @sizes = map { length $_ } @$row;
> maxSizes(\@colWidths,\@sizes);
> push @data, $row;
> }
> for my $row ( @data ) {
> for ( 0 .. $#$row ) {
> printf "$-*s", $colWidths[$_], $row->[$_];
> }
> print "\n";
> }
> note that this is entirely untested.
> I just threw it together in a few short
> coding frenzies…
> also note that it could get even more 'Perl-y' if the arrays
> were turned into hashes but I left it this way
> to keep the memory usage down.
> -dav
>
> On Feb 17, 2012, at 12:28 PM, <mcholste@gmail.com> <mcholste@gmail.com> wrote:
>
> mcholste wrote:
>
> Dear Lord, that is some coding horror. *THIS* is the kind of stuff
> that gives Perl such a bad name. If there is more punctuation than
> alphanumeric chars in your code, you're doing it wrong.
>
> On Thu, Feb 16, 2012 at 6:22 PM, wrote:
>> jt wrote:
>>
>> The Text::CSV_XS module on CPAN can munge pipe delimited data for you, and
>> then give you an iterator to walk to read the data out row by row. I'd go
>> that route.
>>
>>
>> On Feb 16, 2012, at 6:12 PM, wrote:
>>
>> afbach wrote:
>>
>> A 'C' friend wrote this to munge pipe delim sql data into text tables
>> #!/usr/bin/perl
>>
>> die "usage: fmtdump [header1 header2 ...]\n" if ($#ARGV < 0);
>>
>> open(INFILE, "<$ARGV[0]");
>> while () {
>> chomp; @t = split(/\|/);
>> for $i (0 .. $#t) {
>> $c[$i] = length($ARGV[$i+1]) if (! $c[$i]);
>> $t[$i] =~ s/^\s+|\s+$//g;
>> $c[$i] = length($t[$i]) > $c[$i] ? length($t[$i]) : $c[$i];
>> }
>> push @s, [ @t ];
>> }
>>
>> for $i (0 .. $#c) {
>> printf("%-$c[$i]s ", $ARGV[$i+1]);
>> $l .= "-" x ($c[$i] + 1);
>> }
>> print "\n$l\n";
>>
>> for $i (0 .. $#s) {
>> for $j (0 .. $#{$s[$i]}) {
>> printf("%-$c[$j]s ", $s[$i][$j]);
>> }
>> print "\n";
>> }
>> print "\n";
>>
>> The idea being you put the headers on the command line and it handles
>> the spacing and the 2nd line of dashes. It's straightforward but ...
>>
>> a
>> --
>>
>> a
>>
>> Andy Bach,
>> afbach@gmail.com
>> 608 658-1890 cell
>> 608 261-5738 wk
>>
>> View Online
>>
>>
>>
>> Madison Area Perl Mongers - MadMongers
>> http://www.madmongers.org
>>
>>
>> View Online
>>
>>
>>
>> Madison Area Perl Mongers - MadMongers
>> http://www.madmongers.org
>
> View Online
>
> Madison Area Perl Mongers - MadMongers
> http://www.madmongers.org
>
> View Online
>
> Madison Area Perl Mongers - MadMongers
> http://www.madmongers.org
>
Madison Area Perl Mongers - MadMongers
http://www.madmongers.org
Re: got to be a better way
User:
afbach
Date: 2/17/2012 4:48 pm
Date: 2/17/2012 4:48 pm
Views: 0
Rating: 0
Rating: 0
This is what I sent him:
#!/usr/bin/perl
use strict;
use warnings;
die "usage: fmtdump [header1 header2 ...]\n"
unless @ARGV;
my $file = shift @ARGV;
open(INFILE, "<$file") or
die "can't open $file: $!";
my @headers = map { length($_) } @ARGV;
my @data;
while () {
next unless /\w/;
s/\|$//;
my @vals = split(/\|/);
for my $i (0 .. $#vals) {
$vals[$i] =~ s/^\s+|\s+$//g;
$headers[$i] = length($vals[$i])
if not $headers[$i] or $headers[$i] < length($vals[$i]);
}
push @data, \@vals;
}
my $line;
for my $i (@headers) {
printf("%-${i}s ", shift @ARGV)
if @ARGV;
$line .= "-" x ($i + 1);
}
print "\n$line\n";
for my $i (@data) {
for my $j ( 0 .. $#{$i} ) {
printf("%-$headers[$j]s ", ${$i}[$j]);
}
print "\n";
}
print "\n";
--
a
Andy Bach,
afbach@gmail.com
608 658-1890 cell
608 261-5738 wk
#!/usr/bin/perl
use strict;
use warnings;
die "usage: fmtdump [header1 header2 ...]\n"
unless @ARGV;
my $file = shift @ARGV;
open(INFILE, "<$file") or
die "can't open $file: $!";
my @headers = map { length($_) } @ARGV;
my @data;
while () {
next unless /\w/;
s/\|$//;
my @vals = split(/\|/);
for my $i (0 .. $#vals) {
$vals[$i] =~ s/^\s+|\s+$//g;
$headers[$i] = length($vals[$i])
if not $headers[$i] or $headers[$i] < length($vals[$i]);
}
push @data, \@vals;
}
my $line;
for my $i (@headers) {
printf("%-${i}s ", shift @ARGV)
if @ARGV;
$line .= "-" x ($i + 1);
}
print "\n$line\n";
for my $i (@data) {
for my $j ( 0 .. $#{$i} ) {
printf("%-$headers[$j]s ", ${$i}[$j]);
}
print "\n";
}
print "\n";
--
a
Andy Bach,
afbach@gmail.com
608 658-1890 cell
608 261-5738 wk
Re: got to be a better way
User:
zjt
Date: 2/20/2012 9:51 am
Date: 2/20/2012 9:51 am
Views: 0
Rating: 0
Rating: 0
It's akin to how most PHP code sucks because the language attracts web
page designers who don't know how to code.
We could probably use mathematical derivatives to calculate the relative
quality of the installed code for any programming language without
knowing anything about the actual languages.
Jesse
On 2/17/12 2:28 PM, jon@jjminer.org wrote:
> miner wrote:
>
> No, it's what gives C a bad name.
>
> That is not Perl, that is C, written in Perl. And, yes, the Perl sigils
> make it look even worse than the C that it is.
>
> And it's full of poor Perl constructs and traps waiting to catch
> people. My guess is it's written by someone who learned C back when
> storage/memory was really expensive, and who taught themselves Perl and
> just figured out things that "work." ( "while ()" for example. )
>
> jon
>
> On 2/17/12 12:28 PM, mcholste@gmail.com wrote:
>>
>> mcholste wrote:
>>
>> Dear Lord, that is some coding horror. *THIS* is the kind of stuff
>> that gives Perl such a bad name. If there is more punctuation than
>> alphanumeric chars in your code, you're doing it wrong.
>>
>> On Thu, Feb 16, 2012 at 6:22 PM, wrote:
>> > jt wrote:
>> >
>> > The Text::CSV_XS module on CPAN can munge pipe delimited data for
>> you, and
>> > then give you an iterator to walk to read the data out row by row.
>> I'd go
>> > that route.
>> >
>> >
>> > On Feb 16, 2012, at 6:12 PM, wrote:
>> >
>> > afbach wrote:
>> >
>> > A 'C' friend wrote this to munge pipe delim sql data into text tables
>> > #!/usr/bin/perl
>> >
>> > die "usage: fmtdump [header1 header2 ...]\n" if ($#ARGV < 0);
>> >
>> > open(INFILE, "<$ARGV[0]");
>> > while () {
>> > chomp; @t = split(/\|/);
>> > for $i (0 .. $#t) {
>> > $c[$i] = length($ARGV[$i+1]) if (! $c[$i]);
>> > $t[$i] =~ s/^\s+|\s+$//g;
>> > $c[$i] = length($t[$i]) > $c[$i] ? length($t[$i]) : $c[$i];
>> > }
>> > push @s, [ @t ];
>> > }
>> >
>> > for $i (0 .. $#c) {
>> > printf("%-$c[$i]s ", $ARGV[$i+1]);
>> > $l .= "-" x ($c[$i] + 1);
>> > }
>> > print "\n$l\n";
>> >
>> > for $i (0 .. $#s) {
>> > for $j (0 .. $#{$s[$i]}) {
>> > printf("%-$c[$j]s ", $s[$i][$j]);
>> > }
>> > print "\n";
>> > }
>> > print "\n";
>> >
>> > The idea being you put the headers on the command line and it handles
>> > the spacing and the 2nd line of dashes. It's straightforward but ...
>> >
>> > a
>> > --
>> >
>> > a
>> >
>> > Andy Bach,
>> > afbach@gmail.com
>> > 608 658-1890 cell
>> > 608 261-5738 wk
>> >
>> > View Online
>> >
>> >
>> >
>> > Madison Area Perl Mongers - MadMongers
>> > http://www.madmongers.org
>> >
>> >
>> > View Online
>> >
>> >
>> >
>> > Madison Area Perl Mongers - MadMongers
>> > http://www.madmongers.org
>>
>> View Online
>>
>>
>>
>>
>> Madison Area Perl Mongers - MadMongers
>> http://www.madmongers.org
>>
>> --
>> .Jonathan J. Miner----------------------------------------------------.
>> |jon@jjminer.org | photos -http://photos.jjminer.org/ |
>> | | R.A.W. #1629 -http://www.reggaeambassadors.org |
>> | | LOCS Webmaster -http://www.locs-buffett.org |
>> | jabber/gchat:camrycurbhopper@gmail.com |
>> `---------------------------------------------------------------------'
>>
>> "We don't have a town drunk... We all take turns!"
>
> View Online
>
>
>
>
> Madison Area Perl Mongers - MadMongers
> http://www.madmongers.org
page designers who don't know how to code.
We could probably use mathematical derivatives to calculate the relative
quality of the installed code for any programming language without
knowing anything about the actual languages.
Jesse
On 2/17/12 2:28 PM, jon@jjminer.org wrote:
> miner wrote:
>
> No, it's what gives C a bad name.
>
> That is not Perl, that is C, written in Perl. And, yes, the Perl sigils
> make it look even worse than the C that it is.
>
> And it's full of poor Perl constructs and traps waiting to catch
> people. My guess is it's written by someone who learned C back when
> storage/memory was really expensive, and who taught themselves Perl and
> just figured out things that "work." ( "while ()" for example. )
>
> jon
>
> On 2/17/12 12:28 PM, mcholste@gmail.com wrote:
>>
>> mcholste wrote:
>>
>> Dear Lord, that is some coding horror. *THIS* is the kind of stuff
>> that gives Perl such a bad name. If there is more punctuation than
>> alphanumeric chars in your code, you're doing it wrong.
>>
>> On Thu, Feb 16, 2012 at 6:22 PM, wrote:
>> > jt wrote:
>> >
>> > The Text::CSV_XS module on CPAN can munge pipe delimited data for
>> you, and
>> > then give you an iterator to walk to read the data out row by row.
>> I'd go
>> > that route.
>> >
>> >
>> > On Feb 16, 2012, at 6:12 PM, wrote:
>> >
>> > afbach wrote:
>> >
>> > A 'C' friend wrote this to munge pipe delim sql data into text tables
>> > #!/usr/bin/perl
>> >
>> > die "usage: fmtdump [header1 header2 ...]\n" if ($#ARGV < 0);
>> >
>> > open(INFILE, "<$ARGV[0]");
>> > while () {
>> > chomp; @t = split(/\|/);
>> > for $i (0 .. $#t) {
>> > $c[$i] = length($ARGV[$i+1]) if (! $c[$i]);
>> > $t[$i] =~ s/^\s+|\s+$//g;
>> > $c[$i] = length($t[$i]) > $c[$i] ? length($t[$i]) : $c[$i];
>> > }
>> > push @s, [ @t ];
>> > }
>> >
>> > for $i (0 .. $#c) {
>> > printf("%-$c[$i]s ", $ARGV[$i+1]);
>> > $l .= "-" x ($c[$i] + 1);
>> > }
>> > print "\n$l\n";
>> >
>> > for $i (0 .. $#s) {
>> > for $j (0 .. $#{$s[$i]}) {
>> > printf("%-$c[$j]s ", $s[$i][$j]);
>> > }
>> > print "\n";
>> > }
>> > print "\n";
>> >
>> > The idea being you put the headers on the command line and it handles
>> > the spacing and the 2nd line of dashes. It's straightforward but ...
>> >
>> > a
>> > --
>> >
>> > a
>> >
>> > Andy Bach,
>> > afbach@gmail.com
>> > 608 658-1890 cell
>> > 608 261-5738 wk
>> >
>> > View Online
>> >
>> >
>> >
>> > Madison Area Perl Mongers - MadMongers
>> > http://www.madmongers.org
>> >
>> >
>> > View Online
>> >
>> >
>> >
>> > Madison Area Perl Mongers - MadMongers
>> > http://www.madmongers.org
>>
>> View Online
>>
>>
>>
>>
>> Madison Area Perl Mongers - MadMongers
>> http://www.madmongers.org
>>
>> --
>> .Jonathan J. Miner----------------------------------------------------.
>> |jon@jjminer.org | photos -http://photos.jjminer.org/ |
>> | | R.A.W. #1629 -http://www.reggaeambassadors.org |
>> | | LOCS Webmaster -http://www.locs-buffett.org |
>> | jabber/gchat:camrycurbhopper@gmail.com |
>> `---------------------------------------------------------------------'
>>
>> "We don't have a town drunk... We all take turns!"
>
> View Online
>
>
>
>
> Madison Area Perl Mongers - MadMongers
> http://www.madmongers.org
Re: got to be a better way
User:
chrisdolan
Date: 2/20/2012 1:25 pm
Date: 2/20/2012 1:25 pm
Views: 0
Rating: 0
Rating: 0
If you mean "... akin to the *perception of* how most PHP..." then I agree. But if you meant it the way you wrote it, then I strongly disagree. I've seen some really high-quality PHP code in my career, some of it written by people who would label themselves designers.
And as someone who's contributed to a couple of static analysis projects, I also strongly disagree with the latter statement. :-) Static analysis is a lot harder than most people think.
Chris
On Feb 20, 2012, at 9:51 AM, <jesse.thompson@doit.wisc.edu> <jesse.thompson@doit.wisc.edu> wrote:
zjt wrote:
It's akin to how most PHP code sucks because the language attracts web
page designers who don't know how to code.
We could probably use mathematical derivatives to calculate the relative
quality of the installed code for any programming language without
knowing anything about the actual languages.
Jesse
Re: got to be a better way
User:
zjt
Date: 2/20/2012 2:04 pm
Date: 2/20/2012 2:04 pm
Views: 119
Rating: 0
Rating: 0
You kind of missed the premise of my latter conjecture. I wasn't being
very serious, so I now see that I stumbled into the wrong conversation.
Jesse
On 2/20/12 1:25 PM, chris@chrisdolan.net wrote:
> chrisdolan wrote:
>
> If you mean "... akin to the *perception of* how most PHP..." then I
> agree. But if you meant it the way you wrote it, then I strongly
> disagree. I've seen some really high-quality PHP code in my career, some
> of it written by people who would label themselves designers.
>
> And as someone who's contributed to a couple of static analysis
> projects, I also strongly disagree with the latter statement. :-) Static
> analysis is a lot harder than most people think.
>
> Chris
>
>
> On Feb 20, 2012, at 9:51 AM, > > wrote:
>>
>> zjt wrote:
>>
>> It's akin to how most PHP code sucks because the language attracts web
>> page designers who don't know how to code.
>>
>> We could probably use mathematical derivatives to calculate the relative
>> quality of the installed code for any programming language without
>> knowing anything about the actual languages.
>>
>> Jesse
>
> View Online
>
>
>
>
> Madison Area Perl Mongers - MadMongers
> http://www.madmongers.org
very serious, so I now see that I stumbled into the wrong conversation.
Jesse
On 2/20/12 1:25 PM, chris@chrisdolan.net wrote:
> chrisdolan wrote:
>
> If you mean "... akin to the *perception of* how most PHP..." then I
> agree. But if you meant it the way you wrote it, then I strongly
> disagree. I've seen some really high-quality PHP code in my career, some
> of it written by people who would label themselves designers.
>
> And as someone who's contributed to a couple of static analysis
> projects, I also strongly disagree with the latter statement. :-) Static
> analysis is a lot harder than most people think.
>
> Chris
>
>
> On Feb 20, 2012, at 9:51 AM, > > wrote:
>>
>> zjt wrote:
>>
>> It's akin to how most PHP code sucks because the language attracts web
>> page designers who don't know how to code.
>>
>> We could probably use mathematical derivatives to calculate the relative
>> quality of the installed code for any programming language without
>> knowing anything about the actual languages.
>>
>> Jesse
>
> View Online
>
>
>
>
> Madison Area Perl Mongers - MadMongers
> http://www.madmongers.org
Re: got to be a better way
User:
chrisdolan
Date: 2/20/2012 3:55 pm
Date: 2/20/2012 3:55 pm
Views: 0
Rating: 0
Rating: 0
OK, sorry for misunderstanding.
Chris
On Feb 20, 2012, at 2:04 PM, <jesse.thompson@doit.wisc.edu> wrote:
zjt wrote:
You kind of missed the premise of my latter conjecture. I wasn't being
very serious, so I now see that I stumbled into the wrong conversation.
Jesse
On 2/20/12 1:25 PM, chris@chrisdolan.net wrote:
> chrisdolan wrote:
>
> If you mean "... akin to the *perception of* how most PHP..." then I
> agree. But if you meant it the way you wrote it, then I strongly
> disagree. I've seen some really high-quality PHP code in my career, some
> of it written by people who would label themselves designers.
>
> And as someone who's contributed to a couple of static analysis
> projects, I also strongly disagree with the latter statement. :-) Static
> analysis is a lot harder than most people think.
>
> Chris
>
>
> On Feb 20, 2012, at 9:51 AM, > > wrote:
>>
>> zjt wrote:
>>
>> It's akin to how most PHP code sucks because the language attracts web
>> page designers who don't know how to code.
>>
>> We could probably use mathematical derivatives to calculate the relative
>> quality of the installed code for any programming language without
>> knowing anything about the actual languages.
>>
>> Jesse
>
> View Online
>
>
>
>
> Madison Area Perl Mongers - MadMongers
> http://www.madmongers.org
Madison Area Perl Mongers - MadMongers
http://www.madmongers.org