LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-19-2010, 01:33 PM   #1
gregarion
Member
 
Registered: Jan 2010
Posts: 69

Rep: Reputation: 15
Parseing of Information


Hey guys , im a having a problem in figuring out how do i start on my assignment. Basically , i have been given a text file with data arranged in such a way....

Code:
2 
Alice Tay S1234567 4
Procedural CSCI114 3 B
Object CSCI124 3 B
Algorithms CSCI103 4 A
Databases CSCI235 3 A

Jenny Koh S2345678 3
Systems CSCI212 3 C
Algorithms CSCI103 4 A
Databases CSCI235 3 B
The "2" above signifies the number of students in the text file.
The line above the grades signifies the student name , their id number , and the number of subjects they sat. The output expected is...


Code:
Student Name: Alice Tay
Student ID: S1234567
Number of courses enrolled: 4

Course No. Course Name Credits Grade
CSCI103 Algorithms 4 A
CSCI114 Procedural 3 B
CSCI124 Object 3 B
CSCI235 Databases 3 A

Total number of credits: 13



Student Name: Jenny Koh
Student ID: S2345678
Number of courses enrolled: 3

Course No. Course Name Credits Grade
CSCI103 Algorithms 4 A 
CSCI212 Systems 3 C
CSCI235 Databases 3 B

Total number of credits: 10
I am not sure how to go about getting the expected output. From what i see , i have to store each individual information somewhere and then sorting it out based on the subject code. The subject with the lowest subject code should be displayed first then followed by the 2nd lowest and so on.

Hope for some help in how i can start saving and sorting the data out.
 
Old 04-19-2010, 02:09 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by gregarion View Post
Hey guys , im a having a problem in figuring out how do i start on my assignment. Basically , i have been given a text file with data arranged in such a way....

Code:
2 
Alice Tay S1234567 4
Procedural CSCI114 3 B
Object CSCI124 3 B
Algorithms CSCI103 4 A
Databases CSCI235 3 A

Jenny Koh S2345678 3
Systems CSCI212 3 C
Algorithms CSCI103 4 A
Databases CSCI235 3 B
The "2" above signifies the number of students in the text file.
The line above the grades signifies the student name , their id number , and the number of subjects they sat. The output expected is...


Code:
Student Name: Alice Tay
Student ID: S1234567
Number of courses enrolled: 4

Course No. Course Name Credits Grade
CSCI103 Algorithms 4 A
CSCI114 Procedural 3 B
CSCI124 Object 3 B
CSCI235 Databases 3 A

Total number of credits: 13



Student Name: Jenny Koh
Student ID: S2345678
Number of courses enrolled: 3

Course No. Course Name Credits Grade
CSCI103 Algorithms 4 A 
CSCI212 Systems 3 C
CSCI235 Databases 3 B

Total number of credits: 10
I am not sure how to go about getting the expected output. From what i see , i have to store each individual information somewhere and then sorting it out based on the subject code. The subject with the lowest subject code should be displayed first then followed by the 2nd lowest and so on.

Hope for some help in how i can start saving and sorting the data out.
First describe step by step your actions when you do parsing with your eyes and fingers. Hint: you fingers are pointers in a wide sense of the word, i.e. your fingers point to certain places in input file or in memory or in array, etc.
 
Old 04-19-2010, 02:20 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Start with AWK--to get data from fields into variables. As suggested above, having the variable in a array is probably going to be a good idea.

What books or other references are you using? Here are some really good tutorials on AWK and other tools: http://www.grymoire.com/Unix/index.html
Also, got to http://tldp.org and get the Bash Guide for Beginners.
 
Old 04-19-2010, 02:24 PM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
What language are you programming this in?

In that language, do you know how to read a line from a text file into some kind of string variable?

What assumptions are you making about the syntactic rules of the input.

For example, if I were working from just the example (rather than from a reasonable spec)

I would not assume
There is exactly one blank within each student name
There are zero blanks in a subject name.
There is a blank line between students.
There are not blank lines elsewhere.
There are no trailing blanks on the lines.

I would assume
The last blank free chunk of a student line is the number of courses
The second to last blank free chunk of a student line is the student ID.
Everything before that is the student name.
Similarly for last three non blank chunks and everything before that for a course line.

Assumptions like that should be based on a spec, not just an example, so my assumptions might be seriously wrong. But you can't program a parser without making some set of assumptions like that. So decide and document such assumptions and then use them to design the parsing.

Last edited by johnsfine; 04-19-2010 at 02:26 PM.
 
Old 04-19-2010, 02:26 PM   #5
gregarion
Member
 
Registered: Jan 2010
Posts: 69

Original Poster
Rep: Reputation: 15
Sorry , i forgot to add that the assignment is in c++.

Hey sergei, well , from what i see , i first have to use getline to read each individual line. I can use the information given by the text data to find out how many students are there and how many subject each student take. But , from there, i am not very sure how am i able to store the information into an array as i have store different information into different array. For example, i must create an array to store subject code, an array to score the grade , an array to store the credits. Am i on the right track>?
 
Old 04-19-2010, 02:31 PM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
In C, I would define a struct based on the desired format of the OUTPUT. Then, define an array of these structs. As you go thru the lines in the input file, simply assign each field to the appropriate member of the struct.

Are you familiar with structs and arrays in C?
 
Old 04-19-2010, 02:31 PM   #7
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by gregarion View Post
i am not very sure how am i able to store the information into an array as i have store different information into different array.
How much are you supposed to already know and/or be learning in this assignment about STL containers?

An important container to learn about is std::map. You can insert key,object pairs into a std::map in any sequence you like and then you can use an iterator to read them out in sorted sequence.

That is only one (I expect the easiest) solution to the sorting problem in your assignment. There are lots of other ways to sort in C++.

Quote:
For example, i must create an array to store subject code, an array to score the grade , an array to store the credits. Am i on the right track>?
No, not on the right track at all, though that could work.

You should be learning to program with objects. For example, the course name, number of credits and grade for a class taken by a student are a logical collection of data. A non object oriented approach has a separate array for each kind of data. An object oriented approach has a struct for the various data items that go together and then a container of those structs.

The key value (the thing you sort on) is also a logical member of that struct, but one of the compromises made for the ease of use of std::map is to deal with that logical member outside of the struct. There are alternative (slightly harder) sorting methods that would keep the key in its more logical place in the struct with the related data items. (But I would select std::map here because it is easier).

BTW, are the students supposed to be printed out in the sequence read in? Or sorted by ID? Or sorted by (first) name? Your example is unfortunately 3 way ambiguous on that detail.

Last edited by johnsfine; 04-19-2010 at 02:43 PM.
 
Old 04-19-2010, 03:24 PM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by gregarion View Post
Sorry , i forgot to add that the assignment is in c++.

Hey sergei, well , from what i see , i first have to use getline to read each individual line. I can use the information given by the text data to find out how many students are there and how many subject each student take. But , from there, i am not very sure how am i able to store the information into an array as i have store different information into different array. For example, i must create an array to store subject code, an array to score the grade , an array to store the credits. Am i on the right track>?
Struct is more appropriate then array. And, as others have already suggested, do it the OO way. I.e. hide data, though it's still logically equivalent to the struct fields, and in the class to contain the hidden (i.e. private) data write accessors (set*/get* methods).
 
Old 04-20-2010, 12:25 AM   #9
gregarion
Member
 
Registered: Jan 2010
Posts: 69

Original Poster
Rep: Reputation: 15
Well, i read up about structs and i understand how to go about using it. The problem i am having is , how do i define how many struct to set up ? Each student can have more then 4 subjects so im not sure how to go about assigning them. For example, for this assignment my struct will be like this...


Code:
struct Subject {
string CourseName , CourseID , int Credit , string Grade
};
But the problem i face is how do i store all the results of a single student ( example : Alice Tay) to this struct in order to be used?
 
Old 04-20-2010, 12:45 AM   #10
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
If the programming language is C++, why this is not a class?
Give a quick search for classes in C++
 
Old 04-20-2010, 03:57 AM   #11
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by gregarion View Post
Well, i read up about structs and i understand how to go about using it. The problem i am having is , how do i define how many struct to set up ? Each student can have more then 4 subjects so im not sure how to go about assigning them. For example, for this assignment my struct will be like this...


Code:
struct Subject {
string CourseName , CourseID , int Credit , string Grade
};
But the problem i face is how do i store all the results of a single student ( example : Alice Tay) to this struct in order to be used?
You may have array of structs. You may have vector of structs - see STL. Or even vectors of class instances.

Last edited by Sergei Steshenko; 04-20-2010 at 04:03 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find out indone information and datablocks information in a file system chaitanya1982 Linux - Newbie 1 09-24-2008 01:58 AM
Hiding machine information and root information geletine Linux - Security 6 07-14-2006 07:57 AM
where to get information jhv0884 Linux - Newbie 1 09-30-2005 04:08 PM
php parseing config irvken Linux - Software 3 12-09-2004 02:08 PM
User's public_html is not parseing .htaccess files greenhornet Linux - Networking 1 04-02-2002 03:00 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration