Path: csiph.com!usenet.pasdenom.info!news.albasani.net!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: alt.os.linux,alt.os.linux.ubuntu,comp.lang.php Subject: Re: query: how many use PHP for linux scripts Followup-To: comp.lang.php Date: Thu, 15 May 2014 19:33:12 +0200 Organization: PointedEars Software (PES) Lines: 92 Message-ID: <73700029.WZ369GVvbE@PointedEars.de> References: <49ef9$53723273$4b5a0f02$24917@ALLTEL.NET> <130520142135202128%timstreater@greenbee.net> <0.93e9843bc1b5f085d0a5.20140514133906BST.87bnv0edqt.fsf@bsb.me.uk> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: solani.org 1400175197 19573 eJwFwQkBwDAIA0BLUEhG5fAM/xJ6B6OyPyfoWKwBqUT5UDWrix5c2LRI239HJg9kQ/f4RTwV/RDF (15 May 2014 17:33:17 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Thu, 15 May 2014 17:33:17 +0000 (UTC) User-Agent: KNode/4.12.4 X-User-ID: eJwNysEBwEAEBMCWCIsth7vov4Rk3gMLjZMeCMdiX1xnv4Ip+KwMhde87pZnmfbdlNjUam1yIAge/tFYY88HUsgVHQ== Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg== X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. white program Sorry, my programs are mostly in green. Or blue. Depends on editor and language. > the reads a text file and swaps every pair of lines and > writes a new file. > > so > > one > two > three > four > > becomes > > two > one > four > three > > design it to work on any text file. Challenge accepted :) First, read the file's lines into an array: $lines = file('file.txt'); Second, swap the array elements: $swapped = array_map( function ($key) use ($lines) { if ($key % 2 === 0) { return $lines[$key + 1]; } return $lines[$key - 1]; }, array_keys($lines)); Third, write the result: file_put_contents('file-new.txt', join(PHP_EOL, $swapped) . PHP_EOL); Or, in two statements: $lines = file('file.txt'); file_put_contents( 'file-new.txt', join( PHP_EOL, array_map( function ($key) use ($lines) { if ($key % 2 === 0) { return $lines[$key + 1]; } return $lines[$key - 1]; }, array_keys($lines) ) ) . PHP_EOL ); > including files with lines larger than RAM + swap. > if it must work, it's easier in C. I dare you to beat the above *in ease* with C. And where did the maniac grow up whose *text* files demanded those requirements in the first place? Why were they not LARTed when there was still time? PointedEars -- Anyone who slaps a 'this page is best viewed with Browser X' label on a Web page appears to be yearning for the bad old days, before the Web, when you had very little chance of reading a document written on another computer, another word processor, or another network. -- Tim Berners-Lee