473,396 Members | 2,020 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,396 software developers and data experts.

Evaluating array and skipping certain results

I'm having a brain freeze on this. I have a script where I am reading
the contents of a file into an array. I have that up and working with no
problem. Then I'm exploding the array using

while ($line = fgets($fd, 4096))
{
$arrayData = explode("-", trim($line));
}

Works fine. But what I want to do now is evaluate arrayData[2] to see if
it contains the integers "00" (that's zero zero) and if it does, I want
it to skip that entry. I know I need to set it up with
if arrayData[2] == "00" {

But I'm drawing a blank on how to get it to skip this entire line in the
final results. There may be multiple instances of this happening in each
file and I want to skip them all. Can anyone point me in the right
direction for the coding to accomplish this? Thanks.
Jul 17 '05 #1
4 1370
NC
JackM wrote:

I have a script where I am reading the contents of a file into
an array. I have that up and working with no problem. Then I'm
exploding the array using

while ($line = fgets($fd, 4096))
{
$arrayData = explode("-", trim($line));
}

Works fine. But what I want to do now is evaluate arrayData[2]
to see if it contains the integers "00" (that's zero zero) and
if it does, I want it to skip that entry.


That's where continue operator comes in handy:

while ($line = fgets($fd, 4096)) {
$arrayData = explode("-", trim($line));
if (arrayData[2] == '00') {
continue;
}
// do stuff
}

Alternatively, you can simply do:

while ($line = fgets($fd, 4096)) {
$arrayData = explode("-", trim($line));
if (arrayData[2] <> '00') {
// do stuff
}
}

Cheers,
NC

Jul 17 '05 #2
NC <nc@iname.com> wrote:
if (arrayData[2] == '00') {
if (arrayData[2] <> '00') {


A recipie for disaster. If $arrayData[2] equals '0' this evalutates to
true and false resp. If OP really wants to check for '00' he should use
strcmp, === (or !==).

Jul 17 '05 #3
On Fri, 20 May 2005 22:21:34 -0400, JackM wrote:
while ($line = fgets($fd, 4096)) {
$arrayData = explode("-", trim($line));
}


What might be conceptually simpler is

function nonzero($line) {
$a = explode('-', trim($line));
return strcmp('00', $a[2]);
}
$rawdata = file('myfile.ext'); //read into array by lines
$filtered = array_filter($rawdata, 'nonzero');

but it may lead to doing things twice if you need the exploded line
again later. If you only explode() to check for '00', this is probably
fine. If you do need to process each line in its exploded form, you
might want to explode first, then filter:

function expline($line) {
return explode('-', trim($line));
}
function nonzero($arr) {
return strcmp('00', $arr[2]);
}
$rawdata = file('myfile.ext');
$expdata = array_map('expline', $rawdata);
$filtered = array_filter($expdata, 'nonzero');

Then process the elements of $filtered (each element is now an array),
maybe with array_map() again. Nice if you like functional programming.
Shortest imperative version:

$rawdata = file('myfile.ext');
foreach ($rawdata as $line) {
$arr = explode('-', trim($line));
if (strcmp('00', $arr[2])) { //skip if arr[2] is 00
//do stuff with $arr[0], $arr[1], etc.
}
}
--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
Jul 17 '05 #4
Ewoud Dronkert wrote:
Shortest imperative version:

$rawdata = file('myfile.ext');
foreach ($rawdata as $line) {
$arr = explode('-', trim($line));
if (strcmp('00', $arr[2])) { //skip if arr[2] is 00
//do stuff with $arr[0], $arr[1], etc.
}
}


Thanks to all who offered solutions. You people are the best. It seems
that Ewoud's suggestion is the easiest to implement and, better still,
it works for my purposes. ;) I'll have to read up on the continue
control structure to see if it would be suitable for other things I do.
Much obliged to everyone.
Jul 17 '05 #5

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

Similar topics

5
by: NotGiven | last post by:
Here's the pertinent code to get the SQL results. It is generated by dreamweaver adn I'm trying to learn to code php so I don't depend on DW to do it for me. $query_rsSummary = "SELECT sid,...
6
by: Belmin | last post by:
I have an array. The array key is the id of the item (for example categories, 91 is the ID of the category) all ID are not exactly in date order, but they're added to the array in date order, how...
4
by: David Gray | last post by:
Greetings all, I need to sort an array containing text only values and remove duplicates at the same time. I was thinking of... 1. Loading all values into one array (Array1) 2. Read...
9
by: Ken | last post by:
I have a small program , wi tha menu (only option 1,2 and 6 are working for now) Something weird is happening, my array works to input all the data the user puts in, but whebn I choose option 2,...
8
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
4
by: louise raisbeck | last post by:
Hi there, I have created a vb server function which i want to evaluate when a server control button is clicked (only!) however it is evaluating the code on load even though i havent put it into the...
13
by: pozz | last post by:
Is there some modifier that skips a parameter of a printf? For example, I pass three parameters: printf( <format string>, '-', value/10, value%10 ); In certain cases I want to print the minus...
1
by: LittlBUGer | last post by:
Hello. First of all I'm programming in VB.NET/ASP.NET doing a page for a website. Now, to my question.... I have a simple array of integer numbers (15 characters in length) which can hold up to...
3
by: roachic | last post by:
Hi everyone. I am trying to generate a certain number of sliders on my CDialog, that certain number is not fixed. I managed to make the sliders appear by doing this: for certain length{...
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: 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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.