Last active
August 29, 2015 13:55
-
-
Save upsilon/8767388 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$input = new SplFileObject('php://stdin'); | |
$line = $input->fgets(); | |
list($curKey, $sum) = explode(',', $line); | |
while (!$input->eof()) | |
{ | |
$line = $input->fgets(); | |
list($key, $value) = explode(',', $line); | |
if ($key === $curKey) | |
{ | |
$sum += (int)$value; | |
} | |
else | |
{ | |
printf("%s,%d\n", $curKey, $sum); | |
$curKey = $key; | |
$sum = (int)$value; | |
} | |
} | |
printf("%s,%d\n", $curKey, $sum); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
IDENTIFICATION DIVISION. | |
PROGRAM-ID. GROUP-TOTAL. | |
ENVIRONMENT DIVISION. | |
INPUT-OUTPUT SECTION. | |
FILE-CONTROL. | |
SELECT IN-FILE ASSIGN TO "hoge.txt". | |
DATA DIVISION. | |
FILE SECTION. | |
FD IN-FILE. | |
01 IN-REC. | |
02 NAME PIC X(02). | |
02 PIC X(01). | |
02 VAL PIC 9(03). | |
WORKING-STORAGE SECTION. | |
77 EOF PIC 9(01) VALUE ZERO. | |
77 CURNAME PIC X(02). | |
77 TOTAL PIC 9(03). | |
01 OUTLINE. | |
02 OUT-NAME PIC X(02). | |
02 PIC X(01) VALUE ",". | |
02 OUT-VAL PIC 9(03). | |
PROCEDURE DIVISION. | |
OPEN INPUT IN-FILE | |
READ IN-FILE | |
MOVE NAME TO CURNAME | |
MOVE VAL TO TOTAL | |
PERFORM UNTIL EOF = 1 | |
READ IN-FILE | |
AT END | |
MOVE 1 TO EOF | |
NOT AT END | |
IF NAME = CURNAME THEN | |
ADD VAL TO TOTAL | |
ELSE | |
MOVE CURNAME TO OUT-NAME | |
MOVE TOTAL TO OUT-VAL | |
DISPLAY OUTLINE | |
MOVE NAME TO CURNAME | |
MOVE VAL TO TOTAL | |
END-IF | |
END-READ | |
END-PERFORM | |
MOVE CURNAME TO OUT-NAME | |
MOVE TOTAL TO OUT-VAL | |
DISPLAY OUTLINE | |
CLOSE IN-FILE | |
STOP RUN. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment