I was given a file that contains something similar to this kind of structure:
12345 ABC 100M 001 2.0 ABC 1010 4510 A01 451 Apple, Johnny A 150
12345 ABC 100M 011 2.0 ABC 1010 4510 A01 451 Apple, Johnny A 80
12345 ABC 100 011 2.0 ABC 1010 4510 A01 451 Apple, Johnny A 80
I need to grab the following sections from this file:
- Group together the third column (ie. 100M) if they are similar
- Add together the fourth column (if they are in the same group as the third column)
- Add up the last column depending on the fourth column
I've managed to do the following:
$List1 = array();
$grab = fopen("file.txt", "r") or die("Can't open file");
$check = fgets($grab);
while(!feof($grab)) {
if (ereg("^[[:digit:]]{5} +ABC +([[:digit:]]{3}[[:alpha:]]?)+ ([[:digit:]]{3})",
$check, $output)) {
if (!in_array($output[1], $List1)) {
array_push($List1, $output[1]);
}
if (!in_array($output[2], $List1)) {
array_push($List1, $output[2]);
}
}
$check = fgets($grab);
}
fclose($grab);
foreach ($List1 as $list) {
print "$list <br/>";
}
I have managed to somehow group together the third column.
The fourth column is being displayed, but I'm not sure how to group it together into the third column if it's under the same group.
And I'm not sure how to easily grab the last bit in the file/array.
Is there a shortcut to getting the last in a file and adding them up?
Thanks in advance for anyone who can help me.