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
Switch to threaded view of this topic Create a new topic Submit Reply
Re: query: how many use PHP for linux scripts [message #185970 is a reply to message #185967] Thu, 15 May 2014 17:14 Go to previous messageGo to next message
Christoph Michael Bec is currently offline  Christoph Michael Bec
Messages: 207
Registered: June 2013
Karma: 0
Senior Member
The Natural Philosopher wrote:

> On 15/05/14 17:07, crankypuss wrote:
>
>> Read 2 lines, swap, write 2 lines to output file, rinse & repeat, what's
>> the big deal? (Maybe I'm not getting it, I *am* feeling kind of stupid
>> this morning.)
>
> its where you put the lines that wont fit in even virtual memory..
>
> And I think he is making a point.
>
> When the standard tools and assumptions of the language let you down, C
> will get you the solution where PHP will not.

Besides that this exercise seems very contrieved, you could solve it
with PHP as well. There's no need to read whole lines; you can even
read a file character by character if necessary. In this case you'd use
fread(), what's basically the same as stdlib's.

[X post & fup2 comp.lang.php]

--
Christoph M. Becker
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 next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
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
Re: query: how many use PHP for linux scripts [message #185973 is a reply to message #185967] Fri, 16 May 2014 01:45 Go to previous messageGo to next message
Ben Bacarisse is currently offline  Ben Bacarisse
Messages: 82
Registered: November 2013
Karma: 0
Member
The Natural Philosopher <tnp(at)invalid(dot)invalid> writes:
<snip>
> When the standard tools and assumptions of the language let you down,
> C will get you the solution where PHP will not.

Lots of people will get this wrong in both languages, and I am not going
to challenge the suggesting that more PHP programmers that C programmers
might get it wrong, but I am not seeing how this example shows that the
solution is simpler in C.

--
Ben.
Re: query: how many use PHP for linux scripts [message #185974 is a reply to message #185968] Fri, 16 May 2014 07:12 Go to previous messageGo to next message
crankypuss is currently offline  crankypuss
Messages: 147
Registered: March 2011
Karma: 0
Senior Member
On 05/15/2014 10:48 AM, The Natural Philosopher wrote:
> On 15/05/14 17:13, crankypuss wrote:
>> On 05/15/2014 07:52 AM, The Natural Philosopher wrote:
>>> On 15/05/14 13:34, Jasen Betts wrote:
>>>> including files with lines larger than RAM + swap.
>>>
>>> THAT is challenge!
>>>
>>> Have to use temporary files..
>>>
>>>
>>
>> Whatever for? You need enough "disk" space to contain the input file
>> and the output file, you need enough memory to contain the two largest
>> contiguous records
>
> but in the case specified, you dont have that space.
>
>
> and the code to read-2-(swap-2)-write-2. Whether the
>> swap involves actual data movement or not is a nit, it boils down to a
>> file-copy 2 lines at a time. (Wouldn't surprise me if the "cp" command
>> has some option for this kind of thing along with the rest of the
>> kitchen sink... or is that "tar" that I'm thinking of...)
>>
>> Unless of course I'm missing something, I'm feeling stupider with each
>> post about this exercise that I read. <g>
>
> "*lines* larger than RAM + swap" is the key.

Ah, now I see. If you want to make up a difficult problem, that's
swell. Throw in the additional requirement that there are so many
records that there isn't enough "RAM + swap" to contain all the record
offsets. Add an architectural requirement that it support bytes of
infinite and variable length if that makes it more fun.

PHP has access to the same stream-io routines in stdlib that a C
application would use. I have little interest in unsolvable theoretical
problems when so many practical problems remain poorly solved.
Re: query: how many use PHP for linux scripts [message #185975 is a reply to message #185967] Fri, 16 May 2014 07:17 Go to previous messageGo to next message
crankypuss is currently offline  crankypuss
Messages: 147
Registered: March 2011
Karma: 0
Senior Member
On 05/15/2014 10:37 AM, The Natural Philosopher wrote:
> On 15/05/14 17:07, crankypuss wrote:
>> On 05/15/2014 06:34 AM, Jasen Betts wrote:
>>> On 2014-05-14, Ben Bacarisse <ben(dot)usenet(at)bsb(dot)me(dot)uk> wrote:
>>>> The Natural Philosopher <tnp(at)invalid(dot)invalid> writes:
>>>>
>>>> > On 13/05/14 21:35, Tim Streater wrote:
>>>> <snip>
>>>> >> I can't be arsed to fiddle around with C these days. Too much faffing
>>>> >> around with declarations - and no string handling to speak of either.
>>>> >>
>>>> >> Non-issues in PHP.
>>>> >>
>>>> > string handling is the chief reason I prefer C..
>>>>
>>>> That seems odd since PHP has almost every single low-level C string
>>>> function available to it. You can strspn and strncmp to your heart's
>>>> content in PHP. Pretty much the only ones missing are strcat and
>>>> strcpy
>>>> but, surely, they can't be why you prefer C.
>>>>
>>>> A PHP translation of C string handling would inevitably be simpler
>>>> since
>>>> there would be no need manage the storage. That's the biggest
>>>> advantage
>>>> in my opinion, even if never used any of PHP's more complex string
>>>> manipulation.
>>>
>>> white program 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.
>>>
>>>
>>>
>>>
>>> including files with lines larger than RAM + swap.
>>>
>>>
>>>
>>>
>>> if it must work, it's easier in C.
>>>
>>>
>>
>> Read 2 lines, swap, write 2 lines to output file, rinse & repeat, what's
>> the big deal? (Maybe I'm not getting it, I *am* feeling kind of stupid
>> this morning.)
>
> its where you put the lines that wont fit in even virtual memory..
>
>
> And I think he is making a point.
>
> When the standard tools and assumptions of the language let you down, C
> will get you the solution where PHP will not.

I think he's taken you in with a red herring, but I can't see where it
matters one way or another.
Re: query: how many use PHP for linux scripts [message #185976 is a reply to message #185973] Fri, 16 May 2014 08:25 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
On 16/05/14 02:45, Ben Bacarisse wrote:
> The Natural Philosopher <tnp(at)invalid(dot)invalid> writes:
> <snip>
>> When the standard tools and assumptions of the language let you down,
>> C will get you the solution where PHP will not.
>
> Lots of people will get this wrong in both languages, and I am not going
> to challenge the suggesting that more PHP programmers that C programmers
> might get it wrong, but I am not seeing how this example shows that the
> solution is simpler in C.
>

I think the things is probably that C programmers are used to dealing
with more primitive entities than high level programmers are.

PHP is designed to hide the detail by and large.

And 'do the memory management for you'

Which is fine, if phps way of managing memory doesn't crap out.

I had exactly te same sort of issues with mysql. I was trying to
normalise a huge database - 2.5M records as a flat spreadsheet type file
with tons of redundant data in it.

The rather complex SQL query that I end up with still hadnt finished
after 8 hours, so I write a program in C to utilise simpler mysql
queries and then step through them and re interrogate the database
instead of using a nested query structure. And show me what it was doing
as it did it.

The program took 45 minutes to write, and ran in another hour.

The point is that languages that second guess the way you want to do
stuff are never as efficient for a single problem as you can be if you
can decide how yourself.

Most of the time they are mote than good enough: Sometimes they are not.

Its the old 'compiled language versus assembler' argument.

99% of the time is a non issue and you trade the speed of writing code
for the speed of running, but sometime it tips the other way


--
Ineptocracy

(in-ep-toc’-ra-cy) – a system of government where the least capable to
lead are elected by the least capable of producing, and where the
members of society least likely to sustain themselves or succeed, are
rewarded with goods and services paid for by the confiscated wealth of a
diminishing number of producers.
Re: query: how many use PHP for linux scripts [message #185977 is a reply to message #185955] Fri, 16 May 2014 11:29 Go to previous messageGo to next message
Jasen Betts is currently offline  Jasen Betts
Messages: 11
Registered: February 2013
Karma: 0
Junior Member
On 2014-05-15, The Natural Philosopher <tnp(at)invalid(dot)invalid> wrote:
> On 15/05/14 13:34, Jasen Betts wrote:
>> including files with lines larger than RAM + swap.
>
> THAT is challenge!
>
> Have to use temporary files..

You can get these wiith just RAM and lseek64() (etc).


--
umop apisdn


--- news://freenews.netfront.net/ - complaints: news(at)netfront(dot)net ---
Re: query: how many use PHP for linux scripts [message #185978 is a reply to message #185977] Fri, 16 May 2014 12:03 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
On 16/05/14 12:29, Jasen Betts wrote:
> On 2014-05-15, The Natural Philosopher <tnp(at)invalid(dot)invalid> wrote:
>> On 15/05/14 13:34, Jasen Betts wrote:
>>> including files with lines larger than RAM + swap.
>>
>> THAT is challenge!
>>
>> Have to use temporary files..
>
> You can get these wiith just RAM and lseek64() (etc).
>
>
yes..I realised that..collect the metadata, sort it in RAM and then use
that top seek read and write..

Does PHP guarantee 64 bit integers? Or are they stored as text?



--
Ineptocracy

(in-ep-toc’-ra-cy) – a system of government where the least capable to
lead are elected by the least capable of producing, and where the
members of society least likely to sustain themselves or succeed, are
rewarded with goods and services paid for by the confiscated wealth of a
diminishing number of producers.
PHP integers (was: query: how many use PHP for linux scripts) [message #185979 is a reply to message #185978] Fri, 16 May 2014 12:16 Go to previous messageGo to next message
Christoph Michael Bec is currently offline  Christoph Michael Bec
Messages: 207
Registered: June 2013
Karma: 0
Senior Member
The Natural Philosopher wrote:

> Does PHP guarantee 64 bit integers? Or are they stored as text?

That depends on the architecture, see PHP_INT_MAX[1]. Actually,
integers are currently stored as long[2], what might change in the
future[3].

[1] <http://www.php.net/manual/en/reserved.constants.php>
"Usually int(2147483647)" seems a questionable statement, though.
[2] <http://lxr.php.net/xref/PHP_5_5/Zend/zend.h#321>
[3] <https://wiki.php.net/rfc/size_t_and_int64_next#proposal>

[X post & fup2 comp.lang.php]

--
Christoph M. Becker
Re: query: how many use PHP for linux scripts [message #185980 is a reply to message #185969] Fri, 16 May 2014 12:02 Go to previous messageGo to next message
Jasen Betts is currently offline  Jasen Betts
Messages: 11
Registered: February 2013
Karma: 0
Junior Member
On 2014-05-15, Thomas 'PointedEars' Lahn <PointedEars(at)web(dot)de> wrote:
> Chris Ahlstrom wrote:
>
>> crankypuss wrote this copyrighted missive and expects royalties:
>>> On 05/14/2014 03:23 PM, Mike Yetto wrote:
>>>> It would be a neutral thing if it could be ignored, bad if
>>>> enforced.
>>>>
>>>> Mike "would enforcement even be possible?" Yetto
>>>
>>> You mean, kind of like bash is enforced, by being embedded nearly
>>> everywhere from the init system upward?
>>
>> It is? I believe Debian switched to dash for the init system a long time
>> ago.
>
> What is “the init system”?

ls /etc/init.d/* /etc/rc?.d/*
man 8 init

> My Debian GNU/Linux installations all use bash
> (since about 10 years), I am not aware that I had to install anything for
> that. In fact, I had to install dash in order to try it.

howver there is a Debian policy that those scripts be compatible with
a minimal bourne shell.

In debian bash is optional. you can uninstall it and use dash or ash
instead. for a system that sees humans on the command-line daily bash
is the best choice, but for some embedded system wirh few resources
it's probably not the best choice.

> F'up2 comp.unix.shell, please stop cross-hierarchy cross-posting

kind of hard to do as this server doesn't have that group.

--
umop apisdn


--- news://freenews.netfront.net/ - complaints: news(at)netfront(dot)net ---
Re: query: how many use PHP for linux scripts [message #185981 is a reply to message #185976] Fri, 16 May 2014 13:14 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 5/16/2014 4:25 AM, The Natural Philosopher wrote:
> On 16/05/14 02:45, Ben Bacarisse wrote:
>> The Natural Philosopher <tnp(at)invalid(dot)invalid> writes:
>> <snip>
>>> When the standard tools and assumptions of the language let you down,
>>> C will get you the solution where PHP will not.
>>
>> Lots of people will get this wrong in both languages, and I am not going
>> to challenge the suggesting that more PHP programmers that C programmers
>> might get it wrong, but I am not seeing how this example shows that the
>> solution is simpler in C.
>>
>
> I think the things is probably that C programmers are used to dealing
> with more primitive entities than high level programmers are.
>
> PHP is designed to hide the detail by and large.
>
> And 'do the memory management for you'
>
> Which is fine, if phps way of managing memory doesn't crap out.
>
> I had exactly te same sort of issues with mysql. I was trying to
> normalise a huge database - 2.5M records as a flat spreadsheet type file
> with tons of redundant data in it.
>
> The rather complex SQL query that I end up with still hadnt finished
> after 8 hours, so I write a program in C to utilise simpler mysql
> queries and then step through them and re interrogate the database
> instead of using a nested query structure. And show me what it was doing
> as it did it.
>
> The program took 45 minutes to write, and ran in another hour.
>

And if you were competent in SQL, you could have written a simple SQL
statement to do the work in 5 minutes and it would have been done in 15
minutes.

> The point is that languages that second guess the way you want to do
> stuff are never as efficient for a single problem as you can be if you
> can decide how yourself.
>

Or you are as incompetent in SQL as you are in other languages.

> Most of the time they are mote than good enough: Sometimes they are not.
>
> Its the old 'compiled language versus assembler' argument.
>
> 99% of the time is a non issue and you trade the speed of writing code
> for the speed of running, but sometime it tips the other way
>
>


--
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex(at)attglobal(dot)net
==================
Re: query: how many use PHP for linux scripts [message #185982 is a reply to message #185980] Fri, 16 May 2014 14:17 Go to previous messageGo to next message
crankypuss is currently offline  crankypuss
Messages: 147
Registered: March 2011
Karma: 0
Senior Member
On 05/16/2014 06:02 AM, Jasen Betts wrote:
> On 2014-05-15, Thomas 'PointedEars' Lahn <PointedEars(at)web(dot)de> wrote:
>> Chris Ahlstrom wrote:
>>
>>> crankypuss wrote this copyrighted missive and expects royalties:
>>>> On 05/14/2014 03:23 PM, Mike Yetto wrote:
>>>> > It would be a neutral thing if it could be ignored, bad if
>>>> > enforced.
>>>> >
>>>> > Mike "would enforcement even be possible?" Yetto
>>>>
>>>> You mean, kind of like bash is enforced, by being embedded nearly
>>>> everywhere from the init system upward?
>>>
>>> It is? I believe Debian switched to dash for the init system a long time
>>> ago.
>>
>> What is “the init system”?
>
> ls /etc/init.d/* /etc/rc?.d/*
> man 8 init
>
>> My Debian GNU/Linux installations all use bash
>> (since about 10 years), I am not aware that I had to install anything for
>> that. In fact, I had to install dash in order to try it.
>
> howver there is a Debian policy that those scripts be compatible with
> a minimal bourne shell.
>
> In debian bash is optional. you can uninstall it and use dash or ash
> instead. for a system that sees humans on the command-line daily bash
> is the best choice, but for some embedded system wirh few resources
> it's probably not the best choice.
>
>> F'up2 comp.unix.shell, please stop cross-hierarchy cross-posting
>
> kind of hard to do as this server doesn't have that group.
>

Besides that, if you actually read the subject line, you'll see that the
subject topic is cross-hierarchy.
init-system, bash, etc. (was: query: how many use PHP for linux scripts) [message #185983 is a reply to message #185982] Fri, 16 May 2014 16:48 Go to previous message
Christoph Michael Bec is currently offline  Christoph Michael Bec
Messages: 207
Registered: June 2013
Karma: 0
Senior Member
crankypuss wrote:

> On 05/16/2014 06:02 AM, Jasen Betts wrote:
>> On 2014-05-15, Thomas 'PointedEars' Lahn <PointedEars(at)web(dot)de> wrote:
>>> Chris Ahlstrom wrote:
>>>
>>>> crankypuss wrote this copyrighted missive and expects royalties:
>>>> > On 05/14/2014 03:23 PM, Mike Yetto wrote:
>>>> >> It would be a neutral thing if it could be ignored, bad if
>>>> >> enforced.
>>>> >>
>>>> >> Mike "would enforcement even be possible?" Yetto
>>>> >
>>>> > You mean, kind of like bash is enforced, by being embedded nearly
>>>> > everywhere from the init system upward?
>>>>
>>>> It is? I believe Debian switched to dash for the init system a long
>>>> time
>>>> ago.
>>>
>>> What is “the init system”?
>>
>> ls /etc/init.d/* /etc/rc?.d/*
>> man 8 init
>>
>>> My Debian GNU/Linux installations all use bash
>>> (since about 10 years), I am not aware that I had to install anything
>>> for
>>> that. In fact, I had to install dash in order to try it.
>>
>> howver there is a Debian policy that those scripts be compatible with
>> a minimal bourne shell.
>>
>> In debian bash is optional. you can uninstall it and use dash or ash
>> instead. for a system that sees humans on the command-line daily bash
>> is the best choice, but for some embedded system wirh few resources
>> it's probably not the best choice.
>>
>>> F'up2 comp.unix.shell, please stop cross-hierarchy cross-posting
>>
>> kind of hard to do as this server doesn't have that group.
>>
>
> Besides that, if you actually read the subject line, you'll see that the
> subject topic is cross-hierarchy.

This post had absolutely nothing to do with its subject line, though.
As well as lots of other stuff that was posted in this thread with an
unmodified subject line.

And of course this post has nothing--absolutely nothing--to do with PHP.
Please stop spamming comp.lang.php with completely unrelated stuff. TIA.

[X post & fup2 alt.os.linux]

--
Christoph M. Becker
Pages (3): [ «    1  2  3]  Switch to threaded view of this topic Create a new topic Submit Reply
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: Wed Nov 27 06:58:48 GMT 2024

Total time taken to generate the page: 0.02983 seconds