Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re^3: a REFERENCE of array of hashes

by Anonymous Monk
on Jun 24, 2009 at 11:37 UTC ( #774359=note: print w/replies, xml ) Need Help??

in reply to Re^2: a REFERENCE of array of hashes
in thread a REFERENCE of array of hashes

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?

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

    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://774359]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (1)
As of 2023-06-06 23:58 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (29 votes). Check out past polls.