in reply to Re^5: Perl Hashes, keys under keys (I think)?
in thread Perl Hashes, keys under keys (I think)?

I don't quite understand what you are doing here, but generally you ought to avoid nested loops. I suspect this for-for-for can be achieved earlier, when you are first building %records.

But as you have it, confirm what is happening with temporary print statements before each of your assignments:

if (int($colLens[$x]) < length($rec1)) { printf "colLens:%s, rec1:%s\n", $colLens[$x], $rec1; $colLens[$x] = length($rec1); ...

Also, stylistically, instead of:

for (my $y = 0; $y <= $#header; $y++) { $colLens[$y] = length($header[$y]); }

Consider:

@colLens = map length, @header;

I look forward to your final, working program!

Update: I also meant to say, you have int(length(...)) in a few places. length returns an integer, so no need to wrap it with int.

Replies are listed 'Best First'.
Re^7: Perl Hashes, keys under keys (I think)?
by mmartin (Monk) on Sep 22, 2011 at 15:02 UTC

    hbm, thanks for the reply.
    Nice I like that one you suggested: @colLens = map length, @header; cool stuff!


    I had did what you suggested about the print statements inside each looping area, but I had removed them when I copy/pasted it over to the post, just to make it a little easier to read. And what I noticed was that the first 2 loops where giving the correct lengths but everything after that was not.
    Basically, the end result I want is that I will print a table of the records, which will have as many rows as there are records and 8 columns wide.

    And in order to get a nice, readable table printout I'd use this:
    printf "%-10", $elementToPrint;
    I'm sure you know that this will make a "cell" in the table 10 spaces wide and left justified.
    And what I did in another program I wrote was replace "10" with $colLens[$x], where $x is the largest length of the string in each element for every record, including the @header elements as well.


    So for an example, if I have the following data: (I removed a couple of the column elements so it fits on the page correctly)
    The end result should be that @colLens = (9, 14, 7, 8, 3, 7, 8)
    #Max Column length in parenthesis. (9) (14) (7) (8) (3) (7) (8) OWNER | 999?SheetMusic |ralph |50054441 |156 |pts/220 |9:00:44 WAITING-0 | 999?SheetMusic |randall |33096504 |35 |none |9:10:44 WAITING-1 | 999?SheetMusic |kelly |55555556 |999 |pts/900 |9:00:00 OWNER | 001!SCHEDULE |joe |51515154 |000 |tty/100 |10:00:00 WAITING-0 | 001!SCHEDULE |mvick |12345677 |886 |none |9:20:00 WAITING-1 | 001!SCHEDULE |russel |76543215 |456 |tty/101 |10:00:00


    Sorry if I confused you more, trying my best to explain it as well as I can haha.
    Let me know if you think of anything.


    Thanks Again,
    Matt


    .
Re^7: Perl Hashes, keys under keys (I think)?
by mmartin (Monk) on Sep 22, 2011 at 16:11 UTC

    Hey hbm,

    Ok... So I think I figuerd it out.

    Instead of this --> length($rec3) I tried using this --> length($records{ $rec1 }{ $rec2 }{ $rec3 })
    And it seems like its working now!

    I took me a little bit but finally realized that, length($rec3), was giving me the length of the key and not the value.


    Thanks,
    Matt


    .