one-liner

What is your latest useful Perl one-liner (or a pipe involving Perl)?

The one-liner should: solve a real-world problem not be extensively cryptic (should be easy to understand and reproduce) be worth the time it takes to write it (should not be too clever) I'm looking for practical tips and tricks (complementary examples for perldoc perlrun). ...

ColdFusion: pick first non null value from a list

In JavaScript, you can do this: var a = null; var b = "I'm a value"; var c = null; var result = a || b || c; And 'result' will get the value of 'b' because JavaScript short-circuits the 'or' operator. I want a one-line idiom to do this in ColfFusion and the best I can come up with is: <cfif LEN(c) GT 0><cfset result=c></cfif> <cfif ...

What's the best way to tell if your perl's running on -e?

The question is not how to tell in a oneliner. If you're writing the code in a one-liner, you know you are. But how does a module, included by -MMy::Module::Name know that it all started from a oneliner. This is mine. It's non-portable though and relies on UNIX standard commands (although, it can be made portable more or less.) my $pr...

One line code examples in various languages for MD5

I'm looking for one line code examples in various languages for getting a valid MD5 result (as a string, not a bytehash or what have you). For instance: PHP: $token = md5($var1 . $var2); I found VB especially troublesome to do in one line. ...

How do I count the number of occurrences of a char in a String?

I have the string a.b.c.d I want to count the occurrences of '.' in an idiomatic way, preferably a one-liner. (Previously I had expressed this constraint as "without a loop", in case you're wondering why everyone's trying to answer without using a loop). ...

How can I make this Perl one-liner to toggle character in line in a file?

I am attempting to write a one-line Perl script that will toggle a line in a configuration file from "commented" to not and back. I have the following so far: perl -pi -e 's/^(#?)(\tDefaultServerLayout)/ ... /e' xorg.conf I am trying to figure out what code to put in the replacement (...) section. I would like the replacement to inser...

urlencode with only built-in functions

Without using plpgsql, I'm trying to urlencode a given text within a pgsql SELECT statement. The problem with this approach: select regexp_replace('héllo there','([^A-Za-z0-9])','%' || encode(E'\\1','hex'),'g') ...is that the encode function is not passed the regexp parameter, unless there's another way to call functions from within ...

Is there any built-in way to get the length of an iterable in python?

For example, files, in Python, are iterable - they iterate over the lines in the file. I want to count the number of lines. One quick way is to do this: lines = len(list(open(fname))) However, this loads the whole file into memory (at once). This rather defeats the purpose of an iterator (which only needs to keep the current line in...

What are the pros and cons of putting as much logic as possible in a minimum(one-liners) piece of code?

Is it cool? IMO one-liners reduces the readability and makes debugging/understanding more difficult. ...

JavaScript Collection of one-line Useful Functions

This is a question to put as many interesting and useful JavaScript functions written in one line as we can. I made this question because I'm curious how many people around like the art of one-Line programming in JavaScript, and I want to see their progress in action. Put variations of each code inside comments. ...

Filtering and copying with PowerShell

In my quest to improve my PowerShell skills, here's an example of an ugly solution to a simple problem. Any suggestions how to improve the oneliner are welcome. Mission: trim a huge icon library down to something a bit more manageable. The original directory structure looks like this: /Apps and Utilities /Compile /32 Bit A...

How can I make this one-liner work in DOS?

python -c "for x in range(1,10) print x" I enjoy python one liners with -c, but it is limited when indentation is needed. Any ideas? ...

Python one-liner to print every file in the current directory

How can I make the following one liner print every file through Python? python -c "import sys;print '>>',sys.argv[1:]" | dir *.* Specifically would like to know how to pipe into a python -c. DOS or Cygwin responses accepted. ...

Why doesn't my Perl one-liner work on Windows?

I don't have a lot of experience will Perl, but I believe what I am doing should be working. First, from the Windows command prompt I generate a text file of all the files in a directory: dir c:\logfiles /B > config.txt Output: 0001_832ec657.log 0002_a7c8eafc.log I need to feed the "config.txt" file to another executable, but befo...

one liner to extract data block by block

I always deal with data files that consist of many data blocks of the following format: *name* attr ( VALID ( late_lead_up xxx ar uclk reff xxx slope xxx late_lead_dn xxx af uclk reff xxx slope xxx early_trail_up xxx af uclk reff xxx slope xxx early_trail_dn xxx ar ...

Most useful or amazing STL short liners

I'm looking for practical and educational samples of C++ / STL code fitting in few lines. My actual favorites are: Empty a vector freeing its reserved memory: vector <...>().swap (v) (swap with a temporary) Copy a map to a vector: map<T1, T2> myMap; vector< pair<T1, T2> > myVec(myMap.begin(), myMap.end()); // or myVec.assign(myMap....

Powershell script to delete old files

The following script will delete files in a named directory that are older than 14 days and write to a .txt with the path and the files deleted (found this script on another forum..credit to shay): dir c:\tmp -recurse | where {!$.PsIsContainer -AND $.lastWriteTime -lt (Get-Date).AddDays(-14) } | select LastWriteTime,@{n="Path";e={conver...

Initialization of an ArrayList in one line.

I am willing to create a list of options to test something. I was doing: ArrayList<String> places = new ArrayList<String>(); places.add("Buenos Aires"); places.add("Córdoba"); places.add("La Plata"); I refactor the code doing: ArrayList<String> places = new ArrayList<String>(Arrays.asList("Buenos Aires", "Córdoba", "La Plata")); I...

Powershell Golf: Next business day

How to find next business day with powershell ? ...

Is there a one-line function that generates a triangle wave?

In a similar way that modulo generates a sawtooth wave. It doesn't have to be continuous. here is what i mean: int m = 10; int x = 0; int i = 0; while (i < m*3) { printf("%d ", x); x++; x = x % m; i++; } generates a sequence 0..9, three times which looks like this: note that the slope on the right side of the peak is just a gr...