Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #13733

Re: query: how many use PHP for linux scripts

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
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 2014-05-15 19:33 +0200
Organization PointedEars Software (PES)
Message-ID <73700029.WZ369GVvbE@PointedEars.de> (permalink)
References (4 earlier) <lktn4a$n8q$1@news.albasani.net> <130520142135202128%timstreater@greenbee.net> <lkuonh$6na$1@news.albasani.net> <0.93e9843bc1b5f085d0a5.20140514133906BST.87bnv0edqt.fsf@bsb.me.uk> <ll2c99$nr9$4@gonzo.reversiblemaps.ath.cx>

Cross-posted to 3 groups.

Followups directed to: comp.lang.php

Show all headers | View raw


[X-Post & F'up2 comp.lang.php]

Jasen Betts wrote in alt.os.linux and alt.os.linux.ubuntu:

> 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.

<https://en.wikipedia.org/wiki/Moving_the_goalposts>

> 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

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

query: how many use PHP for linux scripts crankypuss <crankypuss@nomail.invalid> - 2014-05-13 02:17 -0600
  Re: query: how many use PHP for linux scripts Arno Welzel <usenet@arnowelzel.de> - 2014-05-13 10:37 +0200
  Re: query: how many use PHP for linux scripts Tim Streater <timstreater@greenbee.net> - 2014-05-13 11:52 +0100
  Re: query: how many use PHP for linux scripts "Auric__" <not.my.real@email.address> - 2014-05-13 13:28 +0000
    Re: query: how many use PHP for linux scripts "Jonathan N. Little" <lws4art@gmail.com> - 2014-05-13 10:29 -0400
  Re: query: how many use PHP for linux scripts Collin Pruitt <hellow+newsgroup@binenv.com> - 2014-05-13 11:00 -0400
    Re: query: how many use PHP for linux scripts crankypuss <crankypuss@nomail.invalid> - 2014-05-13 10:05 -0600
      Re: query: how many use PHP for linux scripts Johnny <johnny@invalid.net> - 2014-05-13 11:32 -0500
        Re: query: how many use PHP for linux scripts The Natural Philosopher <tnp@invalid.invalid> - 2014-05-13 19:09 +0100
          Re: query: how many use PHP for linux scripts Tim Streater <timstreater@greenbee.net> - 2014-05-13 21:35 +0100
            Re: query: how many use PHP for linux scripts JEDIDIAH <jedi@nomad.mishnet> - 2014-05-13 16:10 -0500
              Re: query: how many use PHP for linux scripts Tim Streater <timstreater@greenbee.net> - 2014-05-13 23:03 +0100
                Re: query: how many use PHP for linux scripts Jerry Stuckle <jstucklex@attglobal.net> - 2014-05-13 19:55 -0400
                Re: query: how many use PHP for linux scripts Chris Ahlstrom <OFeem1987@teleworm.us> - 2014-05-13 20:26 -0400
                Re: query: how many use PHP for linux scripts Jerry Stuckle <jstucklex@attglobal.net> - 2014-05-13 20:37 -0400
                Re: query: how many use PHP for linux scripts crankypuss <crankypuss@nomail.invalid> - 2014-05-14 03:18 -0600
                Re: query: how many use PHP for linux scripts Jerry Stuckle <jstucklex@attglobal.net> - 2014-05-14 08:20 -0400
                PHP Gui Toolkits (was: query: how many use PHP for linux scripts) Christoph Michael Becker <cmbecker69@arcor.de> - 2014-05-14 14:58 +0200
                Re: PHP Gui Toolkits crankypuss <crankypuss@nomail.invalid> - 2014-05-15 02:04 -0600
                Re: PHP Gui Toolkits Christoph Michael Becker <cmbecker69@arcor.de> - 2014-05-15 14:38 +0200
                Re: PHP Gui Toolkits The Natural Philosopher <tnp@invalid.invalid> - 2014-05-15 13:59 +0100
                Re: query: how many use PHP for linux scripts crankypuss <crankypuss@nomail.invalid> - 2014-05-15 02:01 -0600
                Re: query: how many use PHP for linux scripts Chris Ahlstrom <OFeem1987@teleworm.us> - 2014-05-14 06:40 -0400
                Re: query: how many use PHP for linux scripts Jerry Stuckle <jstucklex@attglobal.net> - 2014-05-14 08:23 -0400
                Re: query: how many use PHP for linux scripts crankypuss <crankypuss@nomail.invalid> - 2014-05-14 03:00 -0600
                Re: query: how many use PHP for linux scripts JEDIDIAH <jedi@nomad.mishnet> - 2014-05-14 14:49 -0500
              Re: query: how many use PHP for linux scripts crankypuss <crankypuss@nomail.invalid> - 2014-05-14 02:44 -0600
                Re: query: how many use PHP for linux scripts The Natural Philosopher <tnp@invalid.invalid> - 2014-05-14 10:17 +0100
                Re: query: how many use PHP for linux scripts crankypuss <crankypuss@nomail.invalid> - 2014-05-14 03:39 -0600
                Re: query: how many use PHP for linux scripts Tim Streater <timstreater@greenbee.net> - 2014-05-14 12:29 +0100
                Re: query: how many use PHP for linux scripts JEDIDIAH <jedi@nomad.mishnet> - 2014-05-14 14:54 -0500
                Re: query: how many use PHP for linux scripts The Natural Philosopher <tnp@invalid.invalid> - 2014-05-14 21:50 +0100
                Re: query: how many use PHP for linux scripts Jerry Stuckle <jstucklex@attglobal.net> - 2014-05-14 22:49 -0400
                Re: query: how many use PHP for linux scripts JEDIDIAH <jedi@nomad.mishnet> - 2014-05-15 09:45 -0500
                Re: query: how many use PHP for linux scripts The Natural Philosopher <tnp@invalid.invalid> - 2014-05-15 17:35 +0100
                Re: query: how many use PHP for linux scripts crankypuss <crankypuss@nomail.invalid> - 2014-05-15 02:07 -0600
                Re: query: how many use PHP for linux scripts "Jonathan N. Little" <lws4art@gmail.com> - 2014-05-15 07:58 -0400
                Re: query: how many use PHP for linux scripts crankypuss <crankypuss@nomail.invalid> - 2014-05-15 09:55 -0600
                Re: query: how many use PHP for linux scripts Jerry Stuckle <jstucklex@attglobal.net> - 2014-05-15 08:08 -0400
                Re: query: how many use PHP for linux scripts crankypuss <crankypuss@nomail.invalid> - 2014-05-15 10:04 -0600
            Re: query: how many use PHP for linux scripts The Natural Philosopher <tnp@invalid.invalid> - 2014-05-14 04:42 +0100
              Re: query: how many use PHP for linux scripts crankypuss <crankypuss@nomail.invalid> - 2014-05-14 03:21 -0600
                Re: query: how many use PHP for linux scripts The Natural Philosopher <tnp@invalid.invalid> - 2014-05-14 12:32 +0100
              Re: query: how many use PHP for linux scripts Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-05-14 13:39 +0100
                Re: query: how many use PHP for linux scripts The Natural Philosopher <tnp@invalid.invalid> - 2014-05-14 15:23 +0100
                Re: query: how many use PHP for linux scripts Christoph Michael Becker <cmbecker69@arcor.de> - 2014-05-14 20:00 +0200
                Re: query: how many use PHP for linux scripts The Natural Philosopher <tnp@invalid.invalid> - 2014-05-14 21:48 +0100
                Re: query: how many use PHP for linux scripts Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-05-14 22:18 +0100
                Re: query: how many use PHP for linux scripts The Natural Philosopher <tnp@invalid.invalid> - 2014-05-14 23:10 +0100
                Re: query: how many use PHP for linux scripts Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-05-14 22:05 +0100
                Re: query: how many use PHP for linux scripts The Natural Philosopher <tnp@invalid.invalid> - 2014-05-14 23:07 +0100
                Re: query: how many use PHP for linux scripts Jerry Stuckle <jstucklex@attglobal.net> - 2014-05-14 22:51 -0400
                Re: query: how many use PHP for linux scripts Jasen Betts <jasen@xnet.co.nz> - 2014-05-15 12:34 +0000
                Re: query: how many use PHP for linux scripts The Natural Philosopher <tnp@invalid.invalid> - 2014-05-15 14:52 +0100
                Re: query: how many use PHP for linux scripts crankypuss <crankypuss@nomail.invalid> - 2014-05-15 10:13 -0600
                Re: query: how many use PHP for linux scripts The Natural Philosopher <tnp@invalid.invalid> - 2014-05-15 17:48 +0100
                Re: query: how many use PHP for linux scripts crankypuss <crankypuss@nomail.invalid> - 2014-05-16 01:12 -0600
                Re: query: how many use PHP for linux scripts Jasen Betts <jasen@xnet.co.nz> - 2014-05-16 11:29 +0000
                Re: query: how many use PHP for linux scripts The Natural Philosopher <tnp@invalid.invalid> - 2014-05-16 13:03 +0100
                PHP integers (was: query: how many use PHP for linux scripts) Christoph Michael Becker <cmbecker69@arcor.de> - 2014-05-16 14:16 +0200
                Re: query: how many use PHP for linux scripts Jerry Stuckle <jstucklex@attglobal.net> - 2014-05-15 10:03 -0400
                Re: query: how many use PHP for linux scripts Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-05-15 16:26 +0100
                Re: query: how many use PHP for linux scripts crankypuss <crankypuss@nomail.invalid> - 2014-05-15 10:07 -0600
                Re: query: how many use PHP for linux scripts The Natural Philosopher <tnp@invalid.invalid> - 2014-05-15 17:37 +0100
                Re: query: how many use PHP for linux scripts Christoph Michael Becker <cmbecker69@arcor.de> - 2014-05-15 19:14 +0200
                Re: query: how many use PHP for linux scripts Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-05-16 02:45 +0100
                Re: query: how many use PHP for linux scripts The Natural Philosopher <tnp@invalid.invalid> - 2014-05-16 09:25 +0100
                Re: query: how many use PHP for linux scripts Jerry Stuckle <jstucklex@attglobal.net> - 2014-05-16 09:14 -0400
                Re: query: how many use PHP for linux scripts crankypuss <crankypuss@nomail.invalid> - 2014-05-16 01:17 -0600
                Re: query: how many use PHP for linux scripts Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-05-15 19:33 +0200
            Re: query: how many use PHP for linux scripts crankypuss <crankypuss@nomail.invalid> - 2014-05-14 02:36 -0600
              Re: query: how many use PHP for linux scripts The Natural Philosopher <tnp@invalid.invalid> - 2014-05-14 10:09 +0100
                Re: query: how many use PHP for linux scripts crankypuss <crankypuss@nomail.invalid> - 2014-05-14 03:58 -0600
          Re: query: how many use PHP for linux scripts Allodoxaphobia <knock_yourself_out@example.net> - 2014-05-13 21:23 +0000
            Re: query: how many use PHP for linux scripts Mike Yetto <unet.lighthouse@xoxy.net> - 2014-05-13 19:02 -0400
              Re: query: how many use PHP for linux scripts crankypuss <crankypuss@nomail.invalid> - 2014-05-14 03:24 -0600
                Re: query: how many use PHP for linux scripts Mike Yetto <unet.lighthouse@xoxy.net> - 2014-05-14 17:23 -0400
                Re: query: how many use PHP for linux scripts Jerry Stuckle <jstucklex@attglobal.net> - 2014-05-14 22:53 -0400
                Re: query: how many use PHP for linux scripts crankypuss <crankypuss@nomail.invalid> - 2014-05-15 02:13 -0600
                Re: query: how many use PHP for linux scripts Chris Ahlstrom <OFeem1987@teleworm.us> - 2014-05-15 06:09 -0400
                Re: query: how many use PHP for linux scripts crankypuss <crankypuss@nomail.invalid> - 2014-05-15 10:16 -0600
                Re: query: how many use PHP for linux scripts Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-05-15 18:49 +0200
                Re: query: how many use PHP for linux scripts Jasen Betts <jasen@xnet.co.nz> - 2014-05-16 12:02 +0000
                Re: query: how many use PHP for linux scripts crankypuss <crankypuss@nomail.invalid> - 2014-05-16 08:17 -0600
                init-system, bash, etc. (was: query: how many use PHP for linux scripts) Christoph Michael Becker <cmbecker69@arcor.de> - 2014-05-16 18:48 +0200
                Re: query: how many use PHP for linux scripts Mike Yetto <unet.lighthouse@xoxy.net> - 2014-05-15 08:02 -0400
                Re: query: how many use PHP for linux scripts Aragorn <thorongil@telenet.be.invalid> - 2014-05-15 03:16 +0200
        Re: query: how many use PHP for linux scripts crankypuss <crankypuss@nomail.invalid> - 2014-05-13 12:20 -0600
          Re: query: how many use PHP for linux scripts Johnny <johnny@invalid.net> - 2014-05-13 13:35 -0500
            Re: query: how many use PHP for linux scripts Mike Yetto <unet.lighthouse@xoxy.net> - 2014-05-13 19:04 -0400
            Re: query: how many use PHP for linux scripts crankypuss <crankypuss@nomail.invalid> - 2014-05-14 03:26 -0600
              Re: query: how many use PHP for linux scripts Jerry Stuckle <jstucklex@attglobal.net> - 2014-05-14 08:28 -0400
  Re: query: how many use PHP for linux scripts The Natural Philosopher <tnp@invalid.invalid> - 2014-05-13 17:26 +0100

csiph-web