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

Fastest Logic to do this, parse first and last name from string

dlite922
1,584 Expert 1GB
I'm reading a CSV file and the firstname and last name are together separated by a space. (There could be middle name or middle initial as well, but is being ignored)

Since i'll be doing this hundreds of times (for each line of the CSV), I want to make sure I'm doing this logic as fast as I can. This happens a couple of times PER line.

In English:
Extract firstname and lastname from string
if both are empty, error, else insert the one that's not empty (usually first)

Here's my current code:

[PHP]

// Parse the file
fwrite($logFH, 'Parsing File:\nErrors:\n');
$count = 1;
while (($line = fgetcsv($dataFH, 1000, "|")) !== FALSE)
{
// ------------- ADD SALES PERSON
$employeeName = explode(" ",$line[1]);
$firstName = $employeeName[0];
$lastName = (count($employeeName) > 2) ? end($employeeName) : ""; // assumes last name is at the end, ignores middle name or anything in between
$number = $line[0];
if(!empty($number) && (!empty($firstName) || !empty($lastName)))
{
$employeeVO = new EmployeeVO(INVALID,$number , $firstName, $lastName);
$employeeDAO = new DAO($employeeVO);
}
else
{
fwrite($logFH,'Error On Line: $count\n');
continue;
}
.
.
.
}
// closure code
[/PHP]

I just think all the empty() functions are slowing down.

Is there a quick, one-liner, clean way of doing this logic that I (with a hang over) can't seem to do right now?

Thanks fellas,


Dan
Jun 9 '08 #1
2 2408
code green
1,726 Expert 1GB
[PHP]$employeeName = explode(" ",$line[1]);
$firstName = $employeeName[0];
$lastName = (count($employeeName) > 2) ? end($employeeName) : ""; // assumes last name is at the end, ignores middle name or anything in between
$number = $line[0];
if(!empty($number) && (!empty($firstName) || !empty($lastName)))
{[/PHP] The logic is a little skewed.
The string is exploded using spaces and then assumed that all the elements have been created.
What I mean is, it is impossible for the elements to be created if they are empty but you are testing if they are empty.
ergo this comment is meaningless
assumes last name is at the end, ignores middle name or anything in between
There may only be one element.
if you switched on strict error reporting and ran some good test data you will see warning and notice errors, which will slow the code.
explode() is OK here, but think about using isset() rather than count() and empty()
Jun 10 '08 #2
dlite922
1,584 Expert 1GB
[PHP]$employeeName = explode(" ",$line[1]);
$firstName = $employeeName[0];
$lastName = (count($employeeName) > 2) ? end($employeeName) : ""; // assumes last name is at the end, ignores middle name or anything in between
$number = $line[0];
if(!empty($number) && (!empty($firstName) || !empty($lastName)))
{[/PHP] The logic is a little skewed.
The string is exploded using spaces and then assumed that all the elements have been created.
What I mean is, it is impossible for the elements to be created if they are empty but you are testing if they are empty.
ergo this comment is meaningless There may only be one element.
if you switched on strict error reporting and ran some good test data you will see warning and notice errors, which will slow the code.
explode() is OK here, but think about using isset() rather than count() and empty()
thanks, now that i'm "sober" I can clearly see your points.

I actually did do isset() after that. What explode did is converted every space into an array element. so the name arrays were 32 items long.

I ended up just doing isset on the first three for now.
Jun 13 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: ja | last post by:
Hello, I have a table that has a name field with the following data john doe john doe smith john d smith I need to separate the first, middle and last names into separate fields. ex....
60
by: Fotios | last post by:
Hi guys, I have put together a flexible client-side user agent detector (written in js). I thought that some of you may find it useful. Code is here: http://fotios.cc/software/ua_detect.htm ...
2
by: microsoft | last post by:
I have a very "flat" doc structure like this <root> <one> <two> <three> ... n <=100 </root>
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...
11
by: hoopsho | last post by:
Hi Everyone, I am trying to write a program that does a few things very fast and with efficient use of memory... a) I need to parse a space-delimited file that is really large, upwards fo a...
10
by: Extremest | last post by:
I know there are ways to make this a lot faster. Any newsreader does this in seconds. I don't know how they do it and I am very new to c#. If anyone knows a faster way please let me know. All...
4
by: Phil Sandler | last post by:
Hello, What is the fastest/most efficient way of doing string replacement in csharp/.net? An example would be: "Hello, my name is {FirstName}, and I live in the town of {City} with my wife...
1
by: kellysgirl | last post by:
Step 3 – Add code in the Form’s Load event to select the first radio button: In the Form’s Load event, write code to select the Comma choice in the radio buttons group box. Step 4 – Add code in...
5
by: goldtech | last post by:
SAX XML Parse Python error message Hi, My first attempt at SAX, but have an error message I need help with. I cite the error message, code, and xml below. Be grateful if anyone can tell me...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...
0
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...

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.