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

string matching position.

Hello,

I've a string and all sort of values in wich I've to check and return the
correct value.

Here is a sample (simplified as I've a huge amount of datas):

code = 1
subcode 65 values 06101;06102;06103;06106;06108;06109
subcode 69 values 07003;07004;07007;07008;07009;07012;07013

code = 2
subcode 65 values 61215;61218;61558;61587
subcode 69 values 83662:83667;83668

Now I've a given code and value and I need to know the subcode.

I mean if I give the function code=1 and value = 06108 then the function
should return 65
if I give the function code=2 and value = 83668 then the returned subcode
must be 69

How to do so ?

Thanks for helping.

Bob
Mar 26 '08 #1
3 1380
Hi,

You need a small routine to parse the data into an array.

This script will do it:

<?

function parseData($txt)
{
$lines = split("\n", $txt);
$codes = array();
$code = null;

for ($i = 0; $i < count($lines); $i++) {
$line = $lines[$i];
if (preg_match('/^code = (\d+)$/', $line, $m)) {
$code = $m[1];
} elseif (preg_match('/^subcode (\d+) values ((?:\d+;)*(?:\d+))
$/',
$line, $m)) {
if (is_null($code)) {
throw new Exception('No code set at line ' . ($i + 1) . '.');
}
$vals = split(';', $m[2]);
foreach ($vals as $val) {
$codes[$code][$val] = $m[1];
}
} elseif ($line == '') {
if (is_null($code)) {
throw new Exception('Unexpected empty line at line ' .
($i + 1) . '.');
}
$code = null;
} else {
throw new Exception('Unable to process line ' . ($i + 1) . '.');
}
}

return $codes;
}

function getSubcode($codes, $code, $val)
{
return isset($codes[$code]) && isset($codes[$code][$val]) ?
$codes[$code][$val] : false;
}

function test()
{
$txt = <<<end
code = 1
subcode 65 values 06101;06102;06103;06106;06108;06109
subcode 69 values 07003;07004;07007;07008;07009;07012;07013

code = 2
subcode 65 values 61215;61218;61558;61587
subcode 69 values 83662;83667;83668

end;

$codes = parseData($txt);

echo "Subcode for code 1 and value 06108 is " .
getSubcode($codes, 1, '06108') . ".<br>\n";

echo "Subcode for code 2 and value 83668 is " .
getSubcode($codes, 2, '83668') . ".<br>\n";

echo "Subcode for code 3 and value 12345 is " .
getSubcode($codes, 3, '12345') . ".<br>\n";
}

test();

?>

On Mar 26, 12:22 pm, "Bob Bedford" <b...@bedford.comwrote:
Hello,

I've a string and all sort of values in wich I've to check and return the
correct value.

Here is a sample (simplified as I've a huge amount of datas):

code = 1
subcode 65 values 06101;06102;06103;06106;06108;06109
subcode 69 values 07003;07004;07007;07008;07009;07012;07013

code = 2
subcode 65 values 61215;61218;61558;61587
subcode 69 values 83662:83667;83668

Now I've a given code and value and I need to know the subcode.

I mean if I give the function code=1 and value = 06108 then the function
should return 65
if I give the function code=2 and value = 83668 then the returned subcode
must be 69

How to do so ?

Thanks for helping.

Bob
Mar 26 '08 #2
Thanks a lot Peter, with your help I've done it.

"petersprc" <pe*******@gmail.coma écrit dans le message de news:
c3**********************************...oglegroups.com...
Hi,

You need a small routine to parse the data into an array.

This script will do it:

<?

function parseData($txt)
{
$lines = split("\n", $txt);
$codes = array();
$code = null;

for ($i = 0; $i < count($lines); $i++) {
$line = $lines[$i];
if (preg_match('/^code = (\d+)$/', $line, $m)) {
$code = $m[1];
} elseif (preg_match('/^subcode (\d+) values ((?:\d+;)*(?:\d+))
$/',
$line, $m)) {
if (is_null($code)) {
throw new Exception('No code set at line ' . ($i + 1) . '.');
}
$vals = split(';', $m[2]);
foreach ($vals as $val) {
$codes[$code][$val] = $m[1];
}
} elseif ($line == '') {
if (is_null($code)) {
throw new Exception('Unexpected empty line at line ' .
($i + 1) . '.');
}
$code = null;
} else {
throw new Exception('Unable to process line ' . ($i + 1) . '.');
}
}

return $codes;
}

function getSubcode($codes, $code, $val)
{
return isset($codes[$code]) && isset($codes[$code][$val]) ?
$codes[$code][$val] : false;
}

function test()
{
$txt = <<<end
code = 1
subcode 65 values 06101;06102;06103;06106;06108;06109
subcode 69 values 07003;07004;07007;07008;07009;07012;07013

code = 2
subcode 65 values 61215;61218;61558;61587
subcode 69 values 83662;83667;83668

end;

$codes = parseData($txt);

echo "Subcode for code 1 and value 06108 is " .
getSubcode($codes, 1, '06108') . ".<br>\n";

echo "Subcode for code 2 and value 83668 is " .
getSubcode($codes, 2, '83668') . ".<br>\n";

echo "Subcode for code 3 and value 12345 is " .
getSubcode($codes, 3, '12345') . ".<br>\n";
}

test();

?>

On Mar 26, 12:22 pm, "Bob Bedford" <b...@bedford.comwrote:
>Hello,

I've a string and all sort of values in wich I've to check and return the
correct value.

Here is a sample (simplified as I've a huge amount of datas):

code = 1
subcode 65 values 06101;06102;06103;06106;06108;06109
subcode 69 values 07003;07004;07007;07008;07009;07012;07013

code = 2
subcode 65 values 61215;61218;61558;61587
subcode 69 values 83662:83667;83668

Now I've a given code and value and I need to know the subcode.

I mean if I give the function code=1 and value = 06108 then the function
should return 65
if I give the function code=2 and value = 83668 then the returned subcode
must be 69

How to do so ?

Thanks for helping.

Bob


Mar 26 '08 #3
quick&dirty solution

<?php

$data=<<<EOD
code = 1
subcode 65 values 06101;06102;06103;06106;06108;06109
subcode 69 values 07003;07004;07007;07008;07009;07012;07013

code = 2
subcode 65 values 61215;61218;61558;61587
subcode 69 values 83662;83667;83668

EOD;

// search function
function smart_search($data,$code,$value)
{
preg_match("/^code = {$code}[^=]+subcode (\\d+) values
[;\\d]*$value/m",$data,$res);
return isset($res[1]) ? $res[1] : false;
}
// test it
foreach(array('06101'=>1,'06103'=>1,'06109'=>1,'07 009'=>1,'07013'=>1
,'61215'=>2,'61587'=>2,'83668'=>2) as $value=>$code)
echo $value.' '.$code.' : '.smart_search($data,$code,$value)."\n";
?>

I think here was mistake in string 'subcode 69 values 83662:83667;83668'
Mar 26 '08 #4

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

Similar topics

8
by: Jaime Wyant | last post by:
Will someone explain this to me? >>> "test".find("") 0 Why is the empty string found at position 0? Thanks! jw
7
by: spike | last post by:
Im writing a program to search for a string in a binary file. And it works. The problem is: It is sooo slow! how can i make it faster? It takes 27 seconds just to search a 5 meg file. I guess it...
9
by: collinm | last post by:
hi is there a function who allow to compare string and start at position X example str="print.jpp" ext="jpg" i would like to start to compare str at position 6
5
by: olaufr | last post by:
Hi, I'd need to perform simple pattern matching within a string using a list of possible patterns. For example, I want to know if the substring starting at position n matches any of the string I...
4
by: Lauren Wilson | last post by:
Hi folks, We have a need to replace sub strings in certain message text. We use the Office Assistant to display help and often use the imbedded formatting commands. Those of you who have used...
9
by: Jim Lewis | last post by:
Anyone have experience with string pattern matching? I need a fast way to match variables to strings. Example: string - variables ============ abcaaab - xyz abca - xy eeabcac - vxw x...
5
by: int main(void) | last post by:
Hi all, Following is my attempt to write a string search and replace function. #include <stdio.h> #include <stdlib.h> #include <string.h>...
0
NeoPa
by: NeoPa | last post by:
ANSI-89 v ANSI-92 Before we get into all the various types of pattern matching that can be used, there are two ANSI standards used for the main types of wildcard matching (matching zero or more...
8
by: kumarboston | last post by:
Hi All, I have a string of around 500 characters(alphabets) and I have a 3 arrays storing the position "start, end and type" for that string. i am trying to do is to match these position to the...
1
by: Ben | last post by:
I apologize in advance for the newbie question. I'm trying to figure out a way to find all of the occurrences of a regular expression in a string including the overlapping ones. For example,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...

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.