Pseudo code or description


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pseudo code or description
# 1  
Old 04-01-2009
Pseudo code or description

hi I need a Pseudo code or a description of what a program is saying from this spec and code:
Just so i can understand how this solution was achieved, thanks

here is the spec:

Specifications are as follows:-

The books records are stored in one csv file and the layout and the contents are contained in File 1.
There are four categories of books: sports, computing, children's and horror. We should determine the category and then write a series of 3 records to the appropiate file.
The files need to be created in the program and their names will be: children, horror, sports and computing. The format of four files is contained in File 2.

Records which do not belong to any of the above categories should be printed to the screen as an error.

The summary report should be displayed on the screen and will contain the following information:
- Date
- names of files created (including path name)
- total number of boks processed
- total value of books

***************************
File 1 - CSV File sample
ISBN, Title, Category, cost, NO of copies
978-0340681138, Five Get into TRouble (Famous Five), Enid Blyton, Childrens, 6, 3
978-0142301883, The Little Match Girl, Hans Christian Anderson, Childrens, 3, 2
978-0304921532, cell, Stephen King, Horror, 7, 1
978-0596100292, Unix in a Nutshell Scripting, Arnold Robbins, Computing, 17, 2
****************
File 2 - Format of output files
ISBN: 978-0340681138
Author: Enid Blyton
Title: Five Get into Trouble (Famous Five)


And here is a model answer::

Code:
#!/usr/bin/perl
# classify_books.pl
my $csv_file   = shift;
my %categories = ( 'childrens' => 'childrens_books.txt',
                   'horror'    => 'horror_books.txt',
                   'sports   ' => 'sports_books.txt',
                   'computing' => 'computing_books.txt');

my @childrens_file, @horror_file, @sports_file, @computing_file;

open(CSV_FILE, '<', $csv_file)  or  die "Failed to read file $csv_file : $! \n";
while (my $line = <CSV_FILE>) {
    next unless ($line =~ m/^\d{3}-\d+,/);
    my ($isbn, $title, $author, $category) = split(/,/, $line);
    $title    =~ s/^\s+//;
    $author   =~ s/^\s+//;
    $category = lc($category);
    $category =~ s/^\s+//;

    unless (exists($categories{$category})) {
        print STDERR "\nUndefined category $category for book $title by $author having ISBN $isbn \n";
        next;
    }

    if ($category =~ m/childrens/) {
        push(@childrens_file, "ISBN: $isbn\nAuthor: $author\nTitle: $title\n\n");
    }
    elsif ($category =~ m/horror/) {
        push(@horror_file, "ISBN: $isbn\nAuthor: $author\nTitle: $title\n\n");
    }
    elsif ($category =~ m/sports/) {
        push(@sports_file, "ISBN: $isbn\nAuthor: $author\nTitle: $title\n\n");
    }
    elsif ($category =~ m/computing/) {
        push(@computing_file, "ISBN: $isbn\nAuthor: $author\nTitle: $title\n\n");
    }
}
close(CSV_FILE);

if (@childrens_file) {
    open (CHL_FILE, '>', $categories{'childrens'})  or  die "Failed to write file $categories{'childrens'} : $! \n";
    print CHL_FILE @childrens_file;
    close (CHL_FILE);
}

if (@horror_file) {
    open (CHL_FILE, '>', $categories{'horror'})  or  die "Failed to write file $categories{'horror'} : $! \n";
    print CHL_FILE @horror_file;
    close (CHL_FILE);
}

if (@sports_file) {
    open (CHL_FILE, '>', $categories{'sports'})  or  die "Failed to write file $categories{'sports'} : $! \n";
    print CHL_FILE @sports_file;
    close (CHL_FILE);
}

if (@computing_file) {
    open (CHL_FILE, '>', $categories{'computing'})  or  die "Failed to write file $categories{'computing'} : $! \n";
    print CHL_FILE @computing_file;
    close (CHL_FILE);
}

__END__


Last edited by tayyabq8; 04-01-2009 at 08:33 AM.. Reason: Added code tags
# 2  
Old 04-01-2009
Sounds like homework. Also, please to not double post. And [code ][/code ] tags would help a lot...
# 3  
Old 04-01-2009
so what if it is? nobody is born a genius I am working on it myself but a little help won't harm!
# 4  
Old 04-01-2009
I'm not saying that we won't help you if it's homework, even if it's against the rules posting it, as long as you show some initiative. And asking for a complete code example, even if it's just pseudo-code, or asking for a complete rewrite isn't. I hope you understand that this isn't just my "attitude", it's just trying to treat everyone equal (everyone has to abide the rules here) and trying to help you (I found that a little explanation plus thinking for myself helps a lot more that a complete solution)

So, which parts of the code are hard to understand? Or is there any Perl concept that's hard to grasp?
# 5  
Old 04-01-2009
ok sorry that we got off to a bad start, i understand most of the code, but in bash terms the middle bit is confusing, not sure what its trying to say, is it trying to define the categories of the book by reading the cvs file?

Code:
open(CSV_FILE, '<', $csv_file) or die "Failed to read file $csv_file : $! \n";
while (my $line = <CSV_FILE>) {
next unless ($line =~ m/^\d{3}-\d+,/);
my ($isbn, $title, $author, $category) = split(/,/, $line);
$title =~ s/^\s+//;
$author =~ s/^\s+//;
$category = lc($category);
$category =~ s/^\s+//;

and also does push in perl mean the same as path in bash?

Last edited by Yogesh Sawant; 04-01-2009 at 01:29 PM.. Reason: added code tags
# 6  
Old 04-01-2009
Quote:
Originally Posted by ferrycorsten73
ok sorry that we got off to a bad start, [...]
No problem, as long as we both can leave that behind.

Comments are in blue, explaining the next line.
Code:
open(CSV_FILE, '<', $csv_file) or die "Failed to read file $csv_file : $! \n";
# As long as the file didn't end, read a whole line to the variable $line
while (my $line = <CSV_FILE>) {
    # Skip this line unless it begins (^) with 3 digits (\d{3}), a dash, and
    # one or more digits (\d+) up to a ','
    next unless ($line =~ m/^\d{3}-\d+,/);
    # Split the line on the comma, assigning the fragments to the variables
    # in the order they occured
    my ($isbn, $title, $author, $category) = split(/,/, $line);
    # Compress whitespaces
    $title =~ s/^\s+//;
    $author =~ s/^\s+//;
    # Convert to lowercase
    $category = lc($category);
    $category =~ s/^\s+//;

Quote:
Originally Posted by ferrycorsten73
and also does push in perl mean the same as path in bash?
No. push in bash isn't even a builtin, but something provided by the distribution, so don't rely on it being there.
Perls push appends an element to the end of an array (opposite function would be pop, it's pretty simple to implement a stack with those two). Related are shift and unshift if you want to manipulate the start of the array.

If you need more details on a Perl function, call "perldoc -f <function>" on the command line, or look it up on perldoc.perl.org
# 7  
Old 04-01-2009
ok, il take a look, thankyou
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Description using last command

Hello Experts, I need the description (like if user has done sudo,logging from winscp termial or ssh from other machine) using the last command.Can someone help me out please. Thanks. (3 Replies)
Discussion started by: ahmed.vaghar
3 Replies

2. Homework & Coursework Questions

Description of OR AND Operator

From the below i would need the difference using between && and || error_func() { # Standard error function ] && print -u2 "ERROR: $*" exit 1 } ] && error_func "Source Directory is missing as input argument" ] || error_func "Source Directory does not... (1 Reply)
Discussion started by: arun888
1 Replies

3. Linux

May you explain step by step where and how I will add pseudo code

Thank all of you. May you explain step by step where and how I will add pseudo code Note : I have Linux 2.6.24-26-server on x86_64 dears kindly help me (3 Replies)
Discussion started by: nonowa
3 Replies

4. Programming

Change Pseudo Code to C Program (print and fork)

I am very new at programming and this is probably an easy question, but I am desperate. I need to change this low level code into a C program that I can run so I can print out every "A" that appears with the fork() command. You help is greatly appreciated. PRINT A p=fork() if( p == 0) {... (0 Replies)
Discussion started by: tpommm
0 Replies

5. Shell Programming and Scripting

how to write pseudo code

hi, I'm doing some project, I selected shell script as programming language, I need to write pseudo code for my script, can anyone tell me how to write that.......?:) (1 Reply)
Discussion started by: hanaveenkumar
1 Replies

6. Shell Programming and Scripting

pseudo code needs help coding

Thanks in advance!!! Can I get someone to write this small script or can you direct me to a web link, etc. to get it done? --------- Initiate this script every 15 - 20 secs or so through cron. Gather LAN users' $info(username, mac or ipaddr, PID) OBJECT: Tie each username to a mac or ipaddr,... (6 Replies)
Discussion started by: tuxhats
6 Replies

7. Solaris

what is /devices/pseudo/ ??

Hi all, what does this mean? if then <something> fi here is what i know.. it checks if the specified argument no($devid) in some function call is made into a block device and then proceeds with the execution of the loop. However am not understand what lofi@0:means? also is there... (3 Replies)
Discussion started by: wrapster
3 Replies

8. Solaris

pseudo: [ID 129642 kern.info] pseudo-device: vol0

Hi I have a system that gave me some messages on bootup that I was not used to seeing: pseudo: pseudo-device: vol0 genunix: vol0 is /pseudo/vol@0 these came with these: Feb 13 17:42:17 system1 eri: SUNW,eri0 : 100 Mbps full duplex link up Feb 13 17:42:21 system1sendmail: My unqualified... (0 Replies)
Discussion started by: mndavies
0 Replies

9. AIX

2 way and 4 way Hardware Description

Hi, I would like to know the significance of 2 way and 4 way and 8 way Hardware Description, like 2 way p595 and 4 way p695 servers. What exactly does "2 way", "4 way" mean ? (1 Reply)
Discussion started by: praveenr
1 Replies
Login or Register to Ask a Question