473,383 Members | 1,829 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,383 software developers and data experts.

Fastest way to look for an array of char?

Hi,

If I have a string, (variable len), and I am looking for the first position
of one char in array starting from position 'x'

For example,

// the 'haystack'
$string = "PHP is great, php is ok";
// the needles
$char[] = 'P';
$char[] = 'r';
$char[] = 'k';

I would search the $string for the first $char starting from 3 and it would
return 7, (for 'g').

I could use many strpos(...) but that does not sound very efficient.
So what would be the fastest way to get the first position?

And, I am using PHP 4.x, not 5, (otherwise I guess I would use strpos(...)).

Many thanks in advance.

Simon
Jul 17 '05 #1
11 2506
Simon wrote:
Hi,

If I have a string, (variable len), and I am looking for the first
position of one char in array starting from position 'x'

For example,

// the 'haystack'
$string = "PHP is great, php is ok";
// the needles
$char[] = 'P';
$char[] = 'r';
$char[] = 'k';

I would search the $string for the first $char starting from 3 and it
would return 7, (for 'g').
Based on what?
I could use many strpos(...) but that does not sound very efficient.
So what would be the fastest way to get the first position?


If you mean that you want for all characters in char find the minimal
position in $string?

Hash each unique character, with it's position, e.g:

P => 0
H => 1
=> 3
i => 4
s => 5
g => 7
r => 8
e => 9
a => 10
t => 11
, => 12
p => 13
h => 14
o => 20
k => 21

(if I made no mistakes). Then you can do something like:

assign the position of the first item in char to $min
and keep the char in $firstchar;
for the next item in char up until and including the last one:
if the position of this item < $min:
$min = postion
$firstchar = item

It depends also on exactly what you want to do, it's quite possible that
doing just the n searches over the string is faster than the above hash
table stuff.
--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
Jul 17 '05 #2
> Hi,

If I have a string, (variable len), and I am looking for the first
position of one char in array starting from position 'x'

For example,

// the 'haystack'
$string = "PHP is great, php is ok";
// the needles
$char[] = 'P';
$char[] = 'r';
$char[] = 'k';

I would search the $string for the first $char starting from 3 and it
would return 7, (for 'g').

^^^^^^^^^^^^^^^ sorry that should be "return 8 (for 'r')", because i am
looking for 'P', 'r' and 'k', not 'g'.

Simon
Jul 17 '05 #3
>
Hi,

If I have a string, (variable len), and I am looking for the first
position of one char in array starting from position 'x'

For example,

// the 'haystack'
$string = "PHP is great, php is ok";
// the needles
$char[] = 'P';
$char[] = 'r';
$char[] = 'k';

I would search the $string for the first $char starting from 3 and it
would return 7, (for 'g').
Based on what?


Sorry I made a typo, it should have been ...
"I would search the $string for the first $char starting from 3 and it would
return 8, (for 'r')."

'r' been one of the char I looking for.
I could use many strpos(...) but that does not sound very efficient.
So what would be the fastest way to get the first position?


If you mean that you want for all characters in char find the minimal
position in $string?


Sorry the typo made the whole thing unclear.
I want the fastest way to find the position of one of many characters in a
string.

Like look for the first of 'P' , 'r', 'k' in $string.

Simon
Jul 17 '05 #4
Simon wrote:
Hi,

If I have a string, (variable len), and I am looking for the first
position of one char in array starting from position 'x'

For example,

// the 'haystack'
$string = "PHP is great, php is ok";
// the needles
$char[] = 'P';
$char[] = 'r';
$char[] = 'k';

I would search the $string for the first $char starting from 3 and
it would return 7, (for 'g').


Based on what?


Sorry I made a typo, it should have been ...
"I would search the $string for the first $char starting from 3 and it
would return 8, (for 'r')."

'r' been one of the char I looking for.
I could use many strpos(...) but that does not sound very efficient.
So what would be the fastest way to get the first position?


If you mean that you want for all characters in char find the minimal
position in $string?


Sorry the typo made the whole thing unclear.
I want the fastest way to find the position of one of many characters
in a string.

Like look for the first of 'P' , 'r', 'k' in $string.


Fastest way most likely is, just look it up.

If $char is very big, and $string very long (and some characters appear
at the very end for the first time) the hash table trick might be
faster.
--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
Jul 17 '05 #5
>
Fastest way most likely is, just look it up.

If $char is very big, and $string very long (and some characters appear
at the very end for the first time) the hash table trick might be
faster.


$char and $string are never really big, $char is up to 5 chars and $string
at most up to 1024.
I guess I am been overzealous.

I just think that looking for the possible position of 5 chars and returning
the lowest position seems like the long way around.

Simon
Jul 17 '05 #6
Simon wrote:

Fastest way most likely is, just look it up.

If $char is very big, and $string very long (and some characters
appear at the very end for the first time) the hash table trick might
be faster.
$char and $string are never really big, $char is up to 5 chars and
$string at most up to 1024.
I guess I am been overzealous.


So worst case means: 5 x 1024 steps. You can speed this up by going over
the array once, and store the position of each character in a hash, e.g.

for each character in the string:
if character not in hash, use character as key
and position as value.

This means you can now almost in one step (the hash can have some
overhead) look up the position for each item in $char, and hence find
the lowest one:
I just think that looking for the possible position of 5 chars and
returning the lowest position seems like the long way around.


See above. It depends a bit on the overhead of building the hash table,
which technically is just 1024 steps, but it's possible that doing the 5
lookups is faster.

In CS terms, building the hash is O( n ), doing each look up is O( 1 )
so in total the time is O( n ).

Doing 5 look ups in the entire string is 5 x O( n ), and hence total
time is O( n ) too.

Which one is the fastest depends on how PHP does things. Just a loop
like:

minpos = length $string; ( which is always an invalid position)
for i = 0 .. max:
find position of char[ i ]in $string;
if found, and position < minpos:
keep position
keep character

Might be more readable then the possible faster hash approach.

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
Jul 17 '05 #7
"Simon" <sp********@myoddweb.com> wrote in message
news:38*************@individual.net...
Hi,

If I have a string, (variable len), and I am looking for the first position of one char in array starting from position 'x'


What exactly are you trying to do? I suspect regular expression is the
solution here.
Jul 17 '05 #8
>> Hi,

If I have a string, (variable len), and I am looking for the first

position
of one char in array starting from position 'x'


What exactly are you trying to do? I suspect regular expression is the
solution here.


If I wanted to find the pos of a char in a string I would use strpos(...).
But what I want to do is find one of many char in a string.

So ...
// the 'haystack'
$string = "PHP is great, php is ok";
// the needles, (the array of chars I am looking for)
$char[] = 'P';
$char[] = 'r';
$char[] = 'k'

And then I would look for the position of the first possible char in the
string...

In PHP5 you can use strpos(...) with arrays, but what about PHP4?

Speed been important I was wondering what the fastest way might be to look
for one of many char in a string.

I also suspect that RegEx might help, but I am not too good at it.
And what about if I want to start looking from position X rather than search
the whole string?

Simon
Jul 17 '05 #9
"Simon" <sp********@myoddweb.com> wrote in message
news:38*************@individual.net...
If I wanted to find the pos of a char in a string I would use strpos(...).
But what I want to do is find one of many char in a string.

So ...
// the 'haystack'
$string = "PHP is great, php is ok";
// the needles, (the array of chars I am looking for)
$char[] = 'P';
$char[] = 'r';
$char[] = 'k'

And then I would look for the position of the first possible char in the
string...
Surely you'll use the index some where down the line to extract a sub-string
from the text? An index to a string really doesn't have much other use other
than that.
I also suspect that RegEx might help, but I am not too good at it.
And what about if I want to start looking from position X rather than search the whole string?


You can obtain the index of the match by passing the PREG_OFFSET_CAPTURE
flag. To start from a particular position, just specify in the pattern that
the match should have any least x number of any character first:

/(?<=.{8})[Prk]/

Jul 17 '05 #10
>>
And then I would look for the position of the first possible char in the
string...
Surely you'll use the index some where down the line to extract a
sub-string
from the text? An index to a string really doesn't have much other use
other
than that.


Yes, but I use the string for editing the actual string.
So knowing the position of the char is more useful than the string.

But getting PREG_OFFSET_CAPTURE might give me the best of both worlds.
I also suspect that RegEx might help, but I am not too good at it.
And what about if I want to start looking from position X rather than

search
the whole string?


You can obtain the index of the match by passing the PREG_OFFSET_CAPTURE
flag. To start from a particular position, just specify in the pattern
that
the match should have any least x number of any character first:

/(?<=.{8})[Prk]/


sorry i am not sure i follow,

Are you saying that i should do.

preg_match( " /(?<=.{8})[Prk]/", $string, $match, PREG_OFFSET_CAPTURE );

Does "(?<=.{8}") mark the starting posision 8? and [Prk] mean any one char
what if one of the char is a special char like "[" or "{" and so on.

Simon
Jul 17 '05 #11

"Simon" <sp********@myoddweb.com> wrote in message
news:39*************@individual.net...

And then I would look for the position of the first possible char in the string...


Surely you'll use the index some where down the line to extract a
sub-string
from the text? An index to a string really doesn't have much other use
other
than that.


Yes, but I use the string for editing the actual string.
So knowing the position of the char is more useful than the string.

But getting PREG_OFFSET_CAPTURE might give me the best of both worlds.
I also suspect that RegEx might help, but I am not too good at it.
And what about if I want to start looking from position X rather than

search
the whole string?


You can obtain the index of the match by passing the PREG_OFFSET_CAPTURE
flag. To start from a particular position, just specify in the pattern
that
the match should have any least x number of any character first:

/(?<=.{8})[Prk]/


sorry i am not sure i follow,

Are you saying that i should do.

preg_match( " /(?<=.{8})[Prk]/", $string, $match, PREG_OFFSET_CAPTURE );

Does "(?<=.{8}") mark the starting posision 8? and [Prk] mean any one char
what if one of the char is a special char like "[" or "{" and so on.


(?<= ) means a lookbehind assertion, meaning a condition that needs to be
fulfilled previously. In this case we have a condition of .{8}. The dot
means any character. {8} means exactly 8 characters.
Jul 17 '05 #12

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

Similar topics

17
by: DraguVaso | last post by:
Hi, I need to find the FASTEST way to get a string in a Loop, that goes from "a" to "ZZZZZZZZZZZZZZZZZ". So it has to go like this: a b .... z
354
by: Montrose... | last post by:
After working in c# for a year, the only conclusion I can come to is that I wish I knew c. All I need is Linux, the gnu c compiler and I can do anything. Web services are just open sockets...
7
by: bluekite2000 | last post by:
new/delete, std::allocator, pool_allocator or mt_allocator??? What r the pros and cons if each method? PS: I m designing a matrix/tensor/vector lib, all of which call base_constructor of class...
11
by: Ignacio X. Domínguez | last post by:
Hi. I'm developing a desktop application that needs to store some data in a local file. Let's say for example that I want to have an address book with names and phone numbers in a file. I would...
14
by: mesterak | last post by:
I want to very quickly count the number of lines in text files without having to read each line and increment a counter. I am working in VB.NET and C#. Does anyone have a very fast example on how...
9
by: sshock | last post by:
Hi all, I want to read from a file into a vector<unsigned char>. Right now my code looks like this: FILE* f = fopen( "datafile", "rb" ); enum { SIZE = 100 }; vector<unsigned char>...
24
by: ThunderMusic | last post by:
Hi, The subject says it all... I want to use a byte and use it as byte* so I can increment the pointer to iterate through it. What is the fastest way of doing so in C#? Thanks ThunderMusic
19
by: Eugeny Myunster | last post by:
I know, only simple one: #include <stdio.h> int main() { int min=0,max=0,i,arr; for(i=0;i<12;i++) arr=rand()%31-10; for(i=0;i<12;i++)
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.