Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Array problems.....

by khacee (Initiate)
on Mar 19, 2013 at 02:57 UTC ( #1024187=perlquestion: print w/ replies, xml ) Need Help??
khacee has asked for the wisdom of the Perl Monks concerning the following question:

kapp2cape1111 224

kapp2cape1113 224

kapp2cape1112 224

kapp2cape1111 225

kapp2cape1113 225

kapp2cape1112 225

output:

Tableid: 224

Player: kapp2cape1111

kapp2cape1113

kapp2cape1112

Total: 3

Tableid: 225

Player: kapp2cape1111

kapp2cape1113 kapp2cape1112

Total: 3

I want to get this output but i dont know how to start with array. My input was in the .txt.

Comment on Array problems.....
Re: Array problems.....
by NetWallah (Abbot) on Mar 19, 2013 at 03:20 UTC
    This problem is better solved with a hash structure.

    The algorithm would be:

    • Open the file
    • In a loop, read the input
    • chomp, then split the input on white space
    • use the last field as a hash key, and "push" the first field into the HOA (Hash-of-Arrays) structure (Please research HOA at this site)
    • End loop (Keep reading till you have all info)
    • Loop through the collected hash structure, and print the info
    Let us know if you have a specific issue with your implementation of this algorithm.

                 "I'm fairly sure if they took porn off the Internet, there'd only be one website left, and it'd be called 'Bring Back the Porn!'"
            -- Dr. Cox, Scrubs

Re: Array problems.....
by kcott (Abbot) on Mar 19, 2013 at 03:48 UTC

    G'day khacee,

    Welcome to the monastery.

    "I want to get this output but i dont know how to start with array."

    Given your question, I'll assume you've already written the code to read your input file. It's better if you post the code to the point where you got stuck: you'll get a better answer that way. This is all explained in: How do I post a question effectively?

    Each line appears to consist of a "player" and a "tableid". Use the split function to separate these.

    The "tableid" is repeated for multiple "players"; so use that as a hash key pointing to an arrayref of associated "players". Use the push function to add new "players" as you read them from your input.

    It's preferable to show code and data within <code>...</code> tags. You've used <p>...</p> tags here; this makes it difficult to see exactly what formatting you wanted for your output. This is explained in: Markup in the Monastery

    The following commandline code should serve as a starting point for your program. As already mentioned, without knowing how far you managed get before asking for help, it's difficult to know what help to provide.

    $ perl -Mstrict -Mwarnings -E ' my %input; while (<>) { push @{$input{$_->[1]}} => $_->[0] for [split]; } for (sort { $a <=> $b } keys %input) { say "Tableid: $_"; print q{Player: }; say for @{$input{$_}}; say q{Total: }, scalar @{$input{$_}}; } ' kapp2cape1111 224 kapp2cape1113 224 kapp2cape1112 224 kapp2cape1111 225 kapp2cape1113 225 kapp2cape1112 225 Tableid: 224 Player: kapp2cape1111 kapp2cape1113 kapp2cape1112 Total: 3 Tableid: 225 Player: kapp2cape1111 kapp2cape1113 kapp2cape1112 Total: 3

    If you need further help, please read the guidelines I've linked to, and follow them, before posting again.

    -- Ken

Re: Array problems.....
by vinoth.ree (Parson) on Mar 19, 2013 at 04:58 UTC

    Try this too...

    use strict; use warnings; use Data::Dumper; my %hash; while(<DATA>) { my ($player,$tableid) = split; if (exists $hash{$tableid}) { push ($hash{$tableid},$player); } else { $hash{$tableid} = [$player]; } } print Dumper \%hash; while(my($tableid,$player) = each(%hash)) { print "\n\nTableid:$tableid\n"; print "\nPlayer:".join("\n",@{$player}); print "\nTotal:". scalar @{$player}; } __DATA__ kapp2cape1111 224 kapp2cape1113 224 kapp2cape1112 224 kapp2cape1111 225 kapp2cape1113 225 kapp2cape1112 225

    All is well
Re: Array problems.....
by 7stud (Deacon) on Mar 19, 2013 at 04:59 UTC

    I'm guessing that hashes, references, and autovivification are beyond the op's skills at this point. I suggest that you:

    1. Create two arrays: @names224, @names225.
    2. Read each line and chomp() it.
    3. split() the line.
    4. If the second element in the array returned by split() is == to 224, then push() the first element of the array returned by split() onto @names224.
    5. If the second element in the array returned by split() is == to 225, then push() the first element of the array returned by split() onto @names225.
    6. The number of items in an array can be found by assigning the array to a scalar variable.

    Good luck.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (15)
As of 2014-09-02 14:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (25 votes), past polls