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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

printf Always 0 in Last Column

Status
Not open for further replies.

PCHomepage

Programmer
Feb 24, 2009
609
US
As a test of the result of a query, I'm using fprint() to output the values to the screen but no matter how I format it, the last column is always 0 when the data is not. All fields are unsigned integers (BIGINT(50) to be specific).

Any ideas why this is happening? The data was imported from a CSV file: could it be a hidden carriage return and, if so, how can I strip it? formatting as %s shows nothing special there.

shows as:
1 1 1 1 0 0 2 0 0

should be:
1 1 1 1 0 0 2 0 2

printf() formatting as:

Code:
$Format = "%u %u %u %u %u %u %u %u %u \n";
 
is it possible that this is a null terminated string that you are passing in the CSV?

try exploding instead of using printf

Code:
$array = explode(' ', $line);
print_r($array);

equally because you are forcing a conversion to an integer, there could be something oddly appended to the last digit that is forcing the conversion to false (ie zero). like perhaps the line feed is directly after the digit (rather than after the space).

to test try the following

Code:
for ($i=0; $i<strlen($line); $i++):
  echo ord($line[$i]) . "\n";
endfor;

or just to kludge it, don't bother converting to an integer

Code:
$Format = "%s %s %s %s %s %s %s %s %s \n";

or use a regular expression
Code:
$pattern = '/(\d{1})/';
preg_match_all($pattern, $line, $matches);
print_r($matches);
 
Thank you. I had put this issue aside for a couple days but I'm back to it now. Can anyone suggest a query to run to fix the actual problem rather than working around it? Ultimately these will not be printed to the screen but rather that'll be submitted as an array to JPGraph so fixing the actual data is more important than making it look good in a list.

I have not yet had the chance to try the regular expression of your other idea but I original started with the string values and that is how I noticed the problem. As a string, the last column doesn't show up at all.
 
the 'fix' for the problem is in the source data. show us how this is generated and we can fix it.

if the source data cannot be 'fixed' then either of the first, third or fourth code blocks should work. if they do not, then something else is going on and you will need to give us more information (such as the output from the first code block and the second code block)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top