labour
April 2, 2016, 8:58am
1
i have below code,this code should count numbers in file…each line of file have 2 number…i should count unique numbers
like below example
1 390
1 382
1 383
1 357
1 12
1 1
1 376
its contain 7 unique number,but my code says its 8 number, and if file contain below numbers
1 390
1 382
1 383
1 357
1 12
1 13
1 376
here every thing is right my code says its 8 unique number, and its 8 unique number,
what is wrong with This code?
its the code:
<?php
ini_set('max_execution_time', 360);
echo "<br>" ;
//$file = file('web-graph.txt');
ini_set('memory_limit', '2048M');
$sumpage = array();;
$file = fopen( "web-graph.txt", "r" ) or exit ( "Unable to open file!" ) ;
$str = fgets($file);
while ( !feof ( $file ) )
{
$tmp = explode(" ", $str);
$sumpage[$tmp[0]]=1;
$sumpage[$tmp[1]]=1;
$str = fgets($file);
}
echo "<br><br><br><br>";
$x=count($sumpage);
echo "ubique numbers Are=".$x;
fclose( $file ) ;
?>
Can you help?
Try showing the results of the while loop:
function fred($val)
{
echo '<pre>';
print_r($val);
echo '</pre>';
}
ini_set('max_execution_time', 360);
echo "<br>" ;
//$file = file('web-graph.txt');
ini_set('memory_limit', '2048M');
$sumpage = array();;
$file = fopen( "web-graph.txt", "r" ) or exit ( "Unable to open file!" ) ;
$str = fgets($file);
while ( !feof ( $file ) )
{
$tmp = explode(" ", $str);
$sumpage[$tmp[0]]=1;
$sumpage[$tmp[1]]=1;
$str = fgets($file);
fred($sumpage);
}
echo "<br><br><br><br>";
$x=count($sumpage);
echo "ubique numbers Are=".$x;
fclose( $file ) ;
1 Like
labour
April 2, 2016, 3:54pm
5
thanks @John_Betong
i saw your code output with above example,the result is below
Array
(
[1] => 1
[390
] => 1
)
Array
(
[1] => 1
[390
] => 1
[382
] => 1
)
Array
(
[1] => 1
[390
] => 1
[382
] => 1
[383
] => 1
)
Array
(
[1] => 1
[390
] => 1
[382
] => 1
[383
] => 1
[357
] => 1
)
Array
(
[1] => 1
[390
] => 1
[382
] => 1
[383
] => 1
[357
] => 1
[12
] => 1
)
Array
(
[1] => 1
[390
] => 1
[382
] => 1
[383
] => 1
[357
] => 1
[12
] => 1
[1
] => 1
)
Array
(
[1] => 1
[390
] => 1
[382
] => 1
[383
] => 1
[357
] => 1
[12
] => 1
[1
] => 1
[376
] => 1
)
ubique numbers Are=8
i thought indexed array dont repeat indexes,but this one do,
1 Like
labour
April 2, 2016, 3:56pm
6
unfortunately i cant make $sumlink unque in Array_unique() function,
I can’t see how it’s repeating the numbers. On the last output of the array, it lists:
Array ( [1] => 1 [390 ] => 1 [382 ] => 1 [383 ] => 1 [357 ] => 1 [12 ] => 1 [1 ] => 1 [376 ] => 1 )
What’s happening there is that the second one that appears to be index ‘1’ is actually '1 ’ with a space at the end. Change your code to read this:
$sumpage[trim($tmp[0])]=1;
$sumpage[trim($tmp[1])]=1;
But as I mentioned in the other thread, you also need to add code to check how many elements there are in $tmp before you use them, to make sure whether there is 0, 1 or 2 elements. I’m not sure what will happen when there’s only one and you try to create an array element with a null index, I can’t imagine it being good.
1 Like
labour
April 2, 2016, 6:32pm
8
Ahaaaaaa…its because the space in the end, any way to remove space?or get number without space? with string functions?problem is space in the end
labour
April 2, 2016, 7:24pm
9
HI. PROBLEM SOLVED,,
I REMOVED THAT SPACE,AND BINGO
Thanks in advance
2 Likes
See my post #7 - that’s what the trim() function is doing in that revised code.
1 Like
system
Closed
July 4, 2016, 9:49pm
12
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.