in reply to Printing of Array Hash is Missing Elements
How about a little data? Not all the data, just enough to demonstrate the problem. You should also show us a sample of what you get and what you expect for the sample data.
At present I can't tell if there should be data for 40 buildings, but you are only seeing output data for 11, or if the problem is that there are 40 columns in the input and only 11 in the output. Although you describe your output file as CSV, it's not using the same format as the original file and doesn't conform to the usual expectations for a CSV file - it may be better thought of as a text or data file to avoid confusion.
As an aside, don't globally declare local variables - all your temporary variables should be declared where they are initialized (inside the loop or sub). Use the three parameter version of open and use a lexical variable (my $myFile) instead MYFILE (good to see the open is checked though).
Also, use a while loop rather than a for loop for reading input lines.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Printing of Array Hash is Missing Elements
by spickles (Scribe) on Sep 28, 2009 at 20:25 UTC | |
Grandfather - Ok, so I modified some of the data file, as well as the suggestions already posted. It is clear, now, after modifying the building names that the output is only containing unique records. The input is this:
The output is:
| [reply] [d/l] [select] |
by ELISHEVA (Prior) on Sep 28, 2009 at 20:45 UTC | |
Because you are keying your hash by building name, you only get 4 hash entries, one for each unique building name. Each time you add a new line with the same building name, it overwrites the last line you inserted for that building entry. Hence you read in 40 lines but only get out 4. If you want to keep each row separate, I would recommend storing your parsed records in an array of hashes and using a customized sort function. Instead of $buildings{bldg_name}{first_octet} do something like this:
Then in your print out you would use a custom sort to sort the array elements by building name:
See sort and perldsc for more information about custom sorting routines and AoH (array of hash) data structures. Best, beth | [reply] [d/l] [select] |
by ig (Vicar) on Sep 29, 2009 at 09:29 UTC | |
Your code splits on commas but your sample data has no commas. To produce all the networks in the format you have shown, sorted by building and VLAN, I might do something like the following:
But the sorting appears to be unnecessary as the data you have provided is already sorted. | [reply] [d/l] |