FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » query: how many use PHP for linux scripts
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: query: how many use PHP for linux scripts [message #185971 is a reply to message #185954] Thu, 15 May 2014 17:33 Go to previous messageGo to previous message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma:
Senior Member
[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
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: How to get the POST name of a field in a form that uses a counter
Next Topic: PHP-SOAP/5.4.21 SoapClient terminating POST without waiting for reply, but still complaining "error fetching http headers".
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 30 12:41:46 GMT 2024

Total time taken to generate the page: 0.07219 seconds