Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

switch and matching string pattern 2

Status
Not open for further replies.

primate

IS-IT--Management
Jan 6, 2003
123
GB
I've got a multi-dimensional array with an array element containing a long text string.

I need to match a number that represents particular syslog events within the text string eg. 106023. There are about 30 different codes that I want to look for and only one code will appear in each array element.

I thought that using a switch conditional would be the best way to do this but I can't seem ot match the string when doing this.

My code looks like this:

<?

for ($row; $row<$_SESSION['n1']; $row++) {

switch ($querydata[$row]['MsgText']){

case eregi ('106023', $querydata[$row]['MsgText']):

.....some operations....

break;
}
}

?>

The code works when using if statements but I want to use a switch because its more efficient.

What am I doing wrong?



 
In any language switch statement is more effective if (and only if) you have constant choice labels (values). You have not them in this case.
If you don't like if cascade, make an array with pattern strings (ordered by waiting frequences), then scan (with for) it (for every row) to obtain int index, then use switch on this index...
 
then scan (with for) it (for every row) to obtain int index, then use switch on this index..."

Can you elaborate further on this bit please?
 
If you are looking for specific known strings, don't use regular expressions. You use regular expressions only when the searched-for strings can be described as a patter. Instead use strstr() or strpos(), particularly the latter, as they are less resource-intensive.

You might try something like:

Code:
<?php
$codes = array ('106023', '111111', '222222', '333333', '444444');

$fh = fopen ('filename', 'r') or die ('Could not open input file');

while ($line = fgets($fh))
{
   foreach ($codes as $code)
   {
      if (strpos($line, $code) !== FALSE)
      {
         print $line;
      }
   }
}

fclose($fh);


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thanks for pointing that out, I had wondered about that myself - I am a bit of a n00b to php so this is helpful information.
 
The nature of the switch statement is also of interest. You need to consider that switch evaluates the condition of the switch only once (!) and compares it to the values supplied by the case statements.
Code:
switch ($querydata[$row]['MsgText']){
# the above let's say is some text.
#
case eregi ('106023', $querydata[$row]['MsgText']);
# the case statement yields only true or false as result of the eregi() function.
What will be compared here is the original string from the row and the result of the eregi() match. It will always fail unless the MsgText is "1".
 
DRJ478:
That's not exactly true. In PHP, a perl-like switch statement:

switch (TRUE)
{
case ($a == 3 && $b == 2)
do_something();
break;

case ($a == 4)
do_something_else();
break;
}

works, too.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
sleipnir214
Pls. explain what you mean it is not exactly true. IMO my statement still holds with your example:
...switch evaluates the condition of the switch only once...
The expression supplied with the switch is only evaluated once, in your example: just a constant.
...compares it to the values supplied by the case statements...
Also still true. I did not say that the case statement may be a constant or expression.
 
DRJ478 - ah I think see what you are saying - if eregi matches the pattern it returns '1', therefore I am effecively saying "case 1:" ?
 
primate
That is absolutely right. You've got it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top