Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

print and display values

by Anonymous Monk
on Mar 06, 2016 at 22:00 UTC ( [id://1156950]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

have not coded for while, so pardon the rustiness.

I have two sets of values being pulled from a DB.

1 cat1 2 cat1 3 cat2 4 cat3 5 cat3 6 cat1 7 cat1
What I need to do is key off of 2nd filed and associate all values fron field 1. The number of values when reach a pre-defined limit should stop and then start in a new line.
OUTPUT: cat1 1 2 cat1 6 7 cat2 3 cat3 4 5
I've got all the other parts figured out and am stuck on how to make it display as show in the output.

Code tags added by GrandFather

Replies are listed 'Best First'.
Re: print and display values
by NetWallah (Canon) on Mar 06, 2016 at 23:36 UTC
    Please use <code> tags - this will allows the structure in your data to appear properly on this site.

    You need to store the values retrieved into a hash. Assuming the values retrieved in a row are in @row, you can store them as:

    my %cats; # .. loop to get a row # @row contains the retrieved row push $cats{ $row[1] }, $row[0]; # "Hash of arrays structure" # End of loop to get a row
    Now you need to output the info retrieved:
    for my $c (sort keys %cats){ my $values = $cats{ $c }; print "$c\t", join (" ", @$values), "\n"; }
    UPDATE: Adding code needed to split the line after a a specified number of items:
    my $ItemsPerLine=3; for my $c (sort keys %cats){ my $values = $cats{ $c }; print "$c\t"; for my $i(0..$#$values){ $i % $ItemsPerLine==0 and print "\n\t"; print $values->[$i], " "; } print "\n" unless $#$values % $ItemsPerLine==0; }

            "Think of how stupid the average person is, and realize half of them are stupider than that." - George Carlin

Re: print and display values
by Athanasius (Archbishop) on Mar 07, 2016 at 02:59 UTC

    Building on NetWallah’s approach, and adding logic to handle the pre-defined limit:

    #! perl use strict; use warnings; use constant LIMIT => 2; my %cats; while (<DATA>) { my ($value, $key) = split; push @{ $cats{$key} }, $value; } for my $key (sort keys %cats) { my @vals = @{ $cats{$key} }; while (@vals) { print $key; my $count = 0; while (@vals && ++$count <= LIMIT) { print ' ', shift @vals; } print "\n"; } } __DATA__ 1 cat1 2 cat1 3 cat2 4 cat3 5 cat3 6 cat1 7 cat1

    Output:

    12:58 >perl 1565_SoPW.pl cat1 1 2 cat1 6 7 cat2 3 cat3 4 5 12:58 >

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: print and display values
by graff (Chancellor) on Mar 06, 2016 at 23:47 UTC
    It's not clear what you mean by "two sets of values". Since you have "all the other parts figured out", you should show us (an example of) the actual data structure that stores the "two sets of values". (That is, are you using an array of arrays, or a hash, or something else?) Without knowing that, we'd have to guess how the data is stored in your script, and we're likely to be wrong.

    Perhaps you could post a reply with that information.

    When you do, PLEASE put <code> above any/every section of text that contains code and/or data, and also put </code> below each of those sections of text. (These can be abbreviated to <c> ... </c> - if you're curious about this, following the link labeled "Writeup Formatting Tips" conveniently located just above the text entry box where you compose a post.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-24 04:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found