Forum Moderators: coopster

Message Too Old, No Replies

non-comma/tab delimited column separation?

         

drollz

6:10 am on Sep 1, 2003 (gmt 0)

10+ Year Member



non-comma/tab delimited column separation?

Is there anyway you can load data that is not comma or space delimited into a table?

Example: I want to convert the following series of values into colums such as "26", "2", "4", "2", "2", "0", "2", etc.

26 2- 4 2- 2 0- 2 0- 3 2 1 0 1 2 6

IanKelley

8:38 am on Sep 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Run two regular splits, first on the - signs and then on spaces.

lorax

1:58 pm on Sep 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If this string "26 2- 4 2- 2 0- 2 0- 3 2 1 0 1 2 6" is the one to use I'd run a str_replace [us4.php.net] to replace the '-' with '' (no spaces) and then use explode [us4.php.net] to extract the numbers from the string into an array. From there it's a simple matter of accessing the array for your table insert/update.

vincevincevince

6:16 pm on Sep 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if it's shortish (<1Mb-ish) then use lorax's suggestion - for a long dataset parse it charecter by charecter - the preg_replace and explode two passes over a long stream will be expensive otherwise.

$string[$a] ... etc.

IanKelley

9:50 pm on Sep 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Lorax: This method doesn't make sense to me... I haven't tested but I'm fairly sure the processor load/time would be larger than two non preg splits.

Vince: If our friend is storing over a megabyte of data that needs to be parsed later with two delimiters... The overhead of two passes is the least of his worries :-)

However, if two passes makes you unhappy, use a preg split to split based on - or spaces... One pass, simplified logic of a split. This would be faster than running your own code to parse char by char, no?

coopster

10:20 pm on Sep 1, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



drollz, the data you show:
26 2- 4 2- 2 0- 2 0- 3 2 1 0 1 2 6
is space-delimited. Did you mean tab delimited?
Is there anyway you can load data that is not comma or space delimited into a table?

If so, I agree that explode is the function to use. It allows you to specify any separator. However, if you are trying to catch any whitespace separator, then you are probably going to have to do some pattern matching using regular expressions. By the way, if you are showing negative values here, and don't want to, use the
abs
function:

$columns = explode(' ', '26 2- 4 2- 2 0- 2 0- 3 2 1 0 1 2 6');
// or, if trying to match ALL whitespace characters:
$columns = preg_split ("/[\s]+/", '26 2- 4 2- 2 0- 2 0- 3 2 1 0 1 2 6');
while (list($key, $value) = each($columns)) {
$columns[$key] = abs($value);
}

IanKelley

11:31 pm on Sep 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What the discussion has been about, coop, is that he doesn't want the hyphens in his data so it's not a conventional split.

Conceivably a hypen or a space could be the delimiter... Or that's what I assumed in any case, since the hypens have no apparent purpose in the data.

lorax

2:35 am on Sep 2, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Lorax: This method doesn't make sense to me... I haven't tested but I'm fairly sure the processor load/time would be larger than two non preg splits.

I said it would work but I didn't say it was elegant. ;)

The need for more efficiency depends upon how many or how large the string is. If we're talking about a one time event or a modest amount of data it doesn't really matter that much. There's more than one way to achieve the end result.

If, however, we're talking about an ongoing need to parse large strings - yes, it would be wise to write more efficient code.

IanKelley

4:45 am on Sep 2, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



:-)

drollz

9:24 am on Sep 2, 2003 (gmt 0)

10+ Year Member



Wow, guys! Thanks a lot for the help! I think this thread is enough to convince me to subscribe!

I did mean TAB delimited, not space, sorry for the confusion.

I would like to build an admin tool where I can add a row of data such as this and have it upload properly. Would this be possible?

What this dat is, is a line of stats from a basketball boxscore. On a daily basis I want to add such a line for each player who played. My plan is to have an admin tool where I can search by team, all of the players for that team will display, and I can add these lines for each player and then hit SUBMIT.

lorax

12:00 pm on Sep 2, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



drollz,
Isn't that information available in some raw form like XML - perhaps an RSS feed? If it is, you could make your life a whole lot easier by writing a tool that will retrieve the information daily using a Cron job (or even a manual start by you), and parse and insert it into your db.

vincevincevince

1:55 pm on Sep 2, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



drollz - for turning tab seperated values into an array use:
$array=explode("\\t",preg_replace("/\t/","\\t",$string));
or something similar