http://qs1969.pair.com?node_id=774258

fseng has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone, I'm just starting learning Perl. Work give me this exercise to do which I found a bit hard.I need to come back with answers tomorrow afternoon. So answers to any question will be great help please!!!
QUESTION1: Write a program to read in the following comma delimited data file into a data structure of A REFERENCE ARRAY OF HASHES, and performs each of the steps:
Member Number, First Name, Last Name, Subject, Score ----------------------------------------------------- 01,Newton,Issac,Physics,144.56 34,Einstein,Albert,Physics,12.22 78,Feynman,Richard,Physics,-17.34 11,Beethoven,Wolfgang,Music,11.78 89,Merx,Eddie,Cycling,319.01
a) Read in the records and write them out as 15 characters each with the first name in capitals.
b) Print out a sorted list of Surnames only.
c) Print all lines except those with member numbers 34 and 11.
d) Write a new file with the records in reverse order (i.e. print the last record first).
e) Write a new file with only rows where the score is greater than 100.
f) Read in file you made in question a) and write it out as tab delimited records.

QUESTION 2 There are two data files with contents as follows:
client.txt extracts.txt (Member ID, Member Name) (Member ID) ------------------------------- ----------------- 017,Billy Bloggs 024 024,Joe Cocker 099 099,Mozart 182 116,Oliver Bierhoff 182,Francois Mitterand 209,Lois Lane
a) Write code to print only those client details that have their key in extracts.txt. Note that there are (at least) two main ways to do this.
b) Comment on how well your solution scales for files with hundreds of thousands of records (Hints: the extracts list should be much shorter than the whole clients list. How would you approach this question if both data files are huge?)

Replies are listed 'Best First'.
Re: a REFERENCE of array of hashes
by why_bird (Pilgrim) on Jun 24, 2009 at 08:48 UTC

    Hi fseng,

    As Corion has noted, people on this site are not here to do your homework for you. Your previous question at least included the attempt you'd made at the question. Perhaps you could do the same here, or at least ask for references to the things you don't understand. Without knowing what exactly you are struggling with, try:

    Perl was the first language I learnt (and I learnt it at work) so I still remember how daunting it can seem if you've never programmed before. But you still need to learn to ask specific questions (even if they are 'stupid' questions) if you want help here, and, importantly show that you have put work in yourself first. Mention what you have read on the subject, and what you didn't understand even after reading about it. Even better, also let us know what your previous experience with programming/scripting is, if any, then it's easier to tailor the advice we give you.

    Hope this helps, and good luck learning Perl...

    why_bird

    p.s. put these at the top of every Perl script you write. Then at least when something doesn't work you'll have a clue about why.

    use strict; use warnings;

    ........
    Those are my principles. If you don't like them I have others.
    -- Groucho Marx
    .......
      Hi guys,I have tried to work out the answers but the code seems have quiet a few error.I've tried this to create a reference array of hashes:
      use strict; use warnings; use Data::Dumper; my $data = "members.dat"; open (DAT, $data) or die "\"$data\" not existed or can't be opened!\n" +; my @client; my @fields = qw/memberno firstname lastname subjuct score/; while (<DAT>) { next if ( /^Surname/ or /^-/ ); my @values = unpack 'A15A15A15A15A15', $_; my %entry = map { $fields[$_] => $values[$_] } ( 0..$#fields ); push @client, \%entry; }
      I am worndering whether @client I've created is a reference array of hashes? or is it a real array of hashes.
        Also, printf is your friend for printing with specific formatting (like limiting to 15 chars).
        why_bird
        ........
        Those are my principles. If you don't like them I have others.
        -- Groucho Marx
        .......

        I haven't got too much time so a couple of points to start with.

        Learn regular expressions. I mean it. They look like a mess to begin with, but I promise, they're SO useful. I don't really use pack/unpack, so this isn't definitive, but I think they're usually used for 'weird' characters and formatting, or manipulating bits. Well, I'm sure many people will argue with 'usually' and tbh, if they can enlighten me on the matter, it'd be useful..!

        A quick tip to get you going:

        my $text="fakeemail@somewhere.com"; $text =~ /(.+?)@(.+?)/; print "$1, $2"; -------------- fakeemail, somewhere.com
        =~ is the regex operator. the groups, indicated with brackets () that you match show up in the variables $1 and $2.

        For comma separated values, I would also recommend that you use 'split' as I mentioned before. It's much easier and if the format of your fields change, it won't screw up your program.

        @client is an array. A real array. You know it's a real array because you declared it 'my @client'.

        \%entry is a hash reference. So you've made an array of hash references. I'm not sure really what they mean by a 'reference array of hashes' but I would interpret it in the way that you have.

        why_bird
        p.s. I haven't tested the snippets of code here or below, just so you know!
        ........
        Those are my principles. If you don't like them I have others.
        -- Groucho Marx
        .......
        And also what I found confusing is I dont know how to read from a file, modify it and then save it in a new file. Eg. above I unpack the data in to 15 charactor each, push them into an array of hashes. now I want to write them into a new data file with the new fomat. How should I do this?
Re: a REFERENCE of array of hashes
by Corion (Patriarch) on Jun 24, 2009 at 07:44 UTC
    Work give me this exercise to do which I found a bit hard.

    Maybe you should look for a different place of work then.

    This site is not a site where you get programs or homework written for you.

      Hi everyone, I just want to say thanks for all your helps here. I apolergize for trying skip working on the problems myself. I spend the last two days trying to work on those Qs. Now I got all the answers by myself. The feeling is really good. But I know I would not be able to work them all out without the help from you guys. I have learned so much from you. And I start to like Perl now. Thanks again guys.