Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Fail to update an array in HoAoA

by Tanalis (Curate)
on Jul 20, 2005 at 07:52 UTC ( [id://476402]=note: print w/replies, xml ) Need Help??


in reply to Fail to update an array in HoAoA

This seems a little homeworky to me, so I'm not going to just write some code for you. I'll point out a few areas I think you should look at to get a little closer to where you need to be.

Take a closer look at the line

my @KEY = split (/[\s:]/,$_);
If you're trying to split the line on ': ' (colon space), I don't think you want to be using a character class: simply the literal ': ' will do:
my @KEY = split( /: /, $_ );
For data like this set, you could completely replace this split with a regular expression, avoiding the use of the intermediate arrays.

Secondly, think about your @aoa array. As you have it at the minute, it's being redefined each time the while loop iterates. Is that what you need to happen?

Finally, think about your assignment to the %hoa. It's going to the array you have so far to the hash each time the loop iterates - which doesn't seem healthy to me.

I'd consider making use of a second loop, something like this:

for each data line { grab the key loop assign data rows to the array of arrays until there's no more data assign array of arrays to hash }

Using an inner loop in this way will allow you to read the key separately to reading the data, meaning that you can build a complete array of arrays before assigning it to the hash of arrays. This also helps to make your code more readable (in my opinion), and hence more maintainable.

Hope that helps you a little.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-03-19 04:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found