Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: a REFERENCE of array of hashes

by why_bird (Pilgrim)
on Jun 24, 2009 at 08:48 UTC ( #774304=note: print w/replies, xml ) Need Help??

in reply to a REFERENCE of array of hashes

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...


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

Replies are listed 'Best First'.
Re^2: a REFERENCE of array of hashes
by Anonymous Monk on Jun 24, 2009 at 11:05 UTC
    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).
      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=""; $text =~ /(.+?)@(.+?)/; print "$1, $2"; -------------- fakeemail,
      =~ 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.

      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
        Thank you so much for you help. Your hints are very useful. Since I only start learning Perl for the last 2 weeks I dont really know which is the best way to do certain work. Now I can focuse on split to try to work out the answer.
      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?

        You need to specify a 'mode' when you use open. This means that you specify whether you want to read from or write to a particular file (you can do both, but you don't need that for now).

        use strict; use warnings; my $INPUT; my $OUTPUT; open($INPUT, "<", "my_input_file.txt"); #the "<" tells it you want to +read from the file open($OUTPUT, ">" "my_output_file.txt"); #the ">" tells it you want to + write to the file. while(my $line=<$INPUT>){ #.. do some changes to $line .. print $OUTPUT $line #prints $line (after I've done whatever chang +es) to the file I opened as $OUTPUT }

        check out here for some more info on filehandles. Also bear in mind that you can either not declare a filehandle, and just use a plain text word, e.g.  open(INPUT,"<","input_file.txt); or you can declare a variable as I did in the example above. IMO it's a better habit to get into to use a variable, as you start to get into trouble when/if you try to pass filehandles around subs (later..!)

        hope this helps.
        Those are my principles. If you don't like them I have others.
        -- Groucho Marx

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://774304]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2023-03-22 08:28 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (60 votes). Check out past polls.