473,803 Members | 2,934 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2173
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.u k :: 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...@gma il.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
4931
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 happen before 'splitCommands' entered the picture. The problem is in splitCommands( ) somehow modifying the pointer, but I HAVE to call that function. Is there a way to make a copy of it or something ? /* HERE IS MY CODE */ char *...
20
17246
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: 0.0.0.0--->I want to tokenize the string using delimiter as as dot. Regards
8
1932
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 dreams." #include <stdafx.h> #include <string.h> #include <stdio.h> int main()
4
2736
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 somehow confused or upset. I have the following code, which I am using to tokenise some input which is in th form x:y:1.2: int tokenize_input(Sale *sale, char *string){
3
3811
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 <string> #include <stdlib.h> using namespace std;
29
2591
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
904
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", "passwort" from "users" order by "users"
11
17180
by: magicman | last post by:
can anyone point me out to its implementation in C before I roll my own. thx
12
2362
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) 3.4.5 (mingw special) I would like there to be a default, to be returned when two delimiters
0
9703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10555
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10317
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10300
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9127
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7607
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.