dmd command line scripting experiments and observations

Siarhei Siamashka siarhei.siamashka at gmail.com
Sun Jan 28 07:45:14 UTC 2024


On Monday, 25 December 2023 at 11:59:35 UTC, Witold Baryluk wrote:

> For a very long time I have been using bash, grep, sed, awk, 
> usual suspects on Unix, as they are super quick to type, 
> incremental, etc. Once complexity is to big I usually switch to 
> Python (decades ago it might have been Perl or PHP).

I'm actually using Ruby instead of sed, awk and friends for this 
kind of tasks. Python is whitespace sensitive and that's the 
reason why I don't like it in general. But Ruby is essentially a 
modernized Perl with very expressive syntax. Your example with 
spaces removed and single character variables:

`awk 'BEGIN{p=0;m=0.0;}/^mx1/{c=$3;r=c*$4/$9;if(p!=c){print 
p,m;m=0;}p=c;if(r>m)m=r;}END{print p,m;}' < foo.txt`

`ruby -e'g={0=>0};while 
l=gets;l.scan(/^mx1/){a=l.split;c=a[2].to_i;r=c*a[3].to_f/a[8].to_f;g[c]=[g[c]||0,r].max}end;g.each{puts"%d %g"%_1}' < foo.txt`

The following variant works with both Ryby and Crystal:

`crystal eval 'g={0=>0.0};while 
l=gets;l.scan(/^mx1/){a=l.split;c=a[2].to_i;rate=c*a[3].to_f/a[8].to_f;g[c]||=0.0;g[c]=[g[c],rate].max}end;g.each{|v|puts "%d %g"%v}' < foo.txt`

D is not the best language for very terse singleliner 
codegolfing. And it doesn't look like many people are interested 
in adding special syntax sugar tailored for this.


More information about the Digitalmars-d mailing list