473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

strtok() rationale

I downloaded a few PHP scripts from various sites, in part to use them
as is, and partly to study and learn from them.

One script is littered with strtok() occurrences. So I checked the
manual for its details.
I do understand the basics of it, and the limitations (like not being
able to work on two strings simultaneously for loss of the strtok
internal 'pointer').

And it MUST be my ignorance and/or limited experience, but I don't
understand what this function has to offer that can't be solved with
str_split and explode functions, or to be more precise, what typical
problem can better/easier/faster be solved by using strtok() than by
the 'easier' string and array functions.

I am convinced there are good reasons to use strtok, maybe some folks
here can point me to a 'textbook situation' where strtok saves the day
or copy/paste a good self-explanatory example or link to additional
strtok() reading material.

Cheers!
Semi

May 26 '07 #1
3 2154
On 26 May 2007 12:43:24 -0700, se*******@inbox.com wrote:
>One script is littered with strtok() occurrences. So I checked the
manual for its details.
I do understand the basics of it, and the limitations (like not being
able to work on two strings simultaneously for loss of the strtok
internal 'pointer').

And it MUST be my ignorance and/or limited experience, but I don't
understand what this function has to offer that can't be solved with
str_split and explode functions, or to be more precise, what typical
problem can better/easier/faster be solved by using strtok() than by
the 'easier' string and array functions.
It's possible that the author of the code has experience writing string
manipulation code in C, and so was drawn to PHP's string functions that mirror
the standard ones from <string.h>, including strtok, which also inherits the C
function's limitations (most notably, no requirement for reentrancy).

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
May 26 '07 #2
se*******@inbox.com wrote:
I downloaded a few PHP scripts from various sites, in part to use them
as is, and partly to study and learn from them.

One script is littered with strtok() occurrences. So I checked the
manual for its details.
I do understand the basics of it, and the limitations (like not being
able to work on two strings simultaneously for loss of the strtok
internal 'pointer').

And it MUST be my ignorance and/or limited experience, but I don't
understand what this function has to offer that can't be solved with
str_split and explode functions, or to be more precise, what typical
problem can better/easier/faster be solved by using strtok() than by
the 'easier' string and array functions.

I am convinced there are good reasons to use strtok, maybe some folks
here can point me to a 'textbook situation' where strtok saves the day
or copy/paste a good self-explanatory example or link to additional
strtok() reading material.

Cheers!
Semi
strtok and other strxxx functions from C's standard library have the
advantage of speed and smaller memory footprint. For example if you have
a comma-separated list of 1000 elements,

$first = strtok($list, ',')

is apparently much better than

list($first) = explode(',', $list);

Of course, in most real life situations such optimizations are not worth
the trouble, finally it's only a scripting language... but strtok and
friends are a good option for those who want fastest possible code.
--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
May 26 '07 #3
On May 26, 5:50 pm, gosha bine <stereof...@gmail.comwrote:
semi_e...@inbox.com wrote:
I am convinced there are good reasons to use strtok, maybe some folks
here can point me to a 'textbook situation' where strtok saves the day
or copy/paste a good self-explanatory example or link to additional
strtok() reading material.

strtok and other strxxx functions from C's standard library have the
advantage of speed and smaller memory footprint. For example if you have
a comma-separated list of 1000 elements,

$first = strtok($list, ',')

is apparently much better than

list($first) = explode(',', $list);

Of course, in most real life situations such optimizations are not worth
the trouble, finally it's only a scripting language... but strtok and
friends are a good option for those who want fastest possible code.
Is this a shortcut of sorts to memory reference pointers? It feels a
little bit like a string_map function. Scripters have a hard time
molding that problem.

I think it's interesting that in some of the newer languages, the
simplest constructs are abstract ideas that similarly describe the
most systemically atomic memberships: generics, collections, objects.
Scripting is built around flexibility, so everything is everything;
ie, in JavaScript, everything is a generically collectionable object
(consider closures and anonymous functions); once you figure out how
to build classes and constructors, it's like gravy. C#, php, java,
python, etc...

"Simplicity" points at something different in strong-typed languages
than it does in soft-typed interpreted languages.

May 27 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: ern | last post by:
I'm using strtok( ) to capture lines of input. After I call "splitCommand", I call strtok( ) again to get the next line. Strtok( ) returns NULL (but there is more in the file...). That didn't...
20
by: bubunia2000 | last post by:
Hi all, I heard that strtok is not thread safe. So I want to write a sample program which will tokenize string without using strtok. Can I get a sample source code for the same. For exp:...
8
by: hu | last post by:
hi, everybody! I'm testing the fuction of strtok(). The environment is WinXP, VC++6.0. Program is simple, but mistake is confusing. First, the below code can get right outcome:"ello world, hello...
4
by: Michael | last post by:
Hi, I have a proble I don't understand when using strtok(). It seems that if I make a call to strtok(), then make a call to another function that also makes use of strtok(), the original call is...
3
by: nomad5000 | last post by:
Hi everybody! I'm having trouble using strtok to fill a matrix with int nrs. from a file. the code that is not working is the following: #include <iostream> #include <fstream> #include...
29
by: Pietro Cerutti | last post by:
Hello, here I have a strange problem with a real simple strtok example. The program is as follows: ### BEGIN STRTOK ### #include <string.h> #include <stdio.h>
11
by: Lothar Behrens | last post by:
Hi, I have selected strtok to be used in my string replacement function. But I lost the last token, if there is one. This string would be replaced select "name", "vorname", "userid",...
11
by: magicman | last post by:
can anyone point me out to its implementation in C before I roll my own. thx
12
by: Pilcrow | last post by:
Here is a quick program, together with its output, that illustrates what I consider to be a deficiency of the standard function strtok from <string.h>: I am using C:\>gcc --version gcc (GCC)...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.