Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Keyed matrix

by Anonymous Monk
on Sep 03, 2012 at 11:50 UTC ( [id://991398]=perlquestion: print w/replies, xml ) Need Help??

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

I am asked to create a 3 by 7 matrix in perl with a specified key for each row.I have heard about hashes with keys.Is matrix is also having key?? For eg: Monday dishes 1 Tuesday vacuum 2 Wednesday garbage 3

%chores = ("Monday", "dishes","1", "Tuesday", "vacuum","2", "Wednesday +", "garbage","3"); $value = $chores{Wednesday}; print "$value";
Here outbut is garbage alone.What if i need to get both garbage and 3 if i give the key Wednesday?Is it possible in hash? </code>

Replies are listed 'Best First'.
Re: Keyed matrix
by Marshall (Canon) on Sep 03, 2012 at 12:09 UTC
    That is a very "unPerl" idea ( that 3 by 7 array). Perhaps your Perl instructor doesn't understand Perl or perhaps you don't understand the assignment. Ask a question about how to accomplish a general task and you will get a lot of help.

    Update:

    #!/usr/bin/perl -w use strict; #Ok, here is an idea for you... my %scores = ( "Monday" => ["dishes","1"], "Tuesday" => ["vacuum","2"], "Wednesday" => ["garbage","3"], ); print "@{$scores{Wednesday}}\n"; __END__ Prints: garbage 3
Re: Keyed matrix
by choroba (Cardinal) on Sep 03, 2012 at 12:17 UTC
    What you defined is not what you need:
    %chores = (Monday => "dishes", 1 => "Tuesday", vacuum => "2", Wednesday => "garbage", "3");
    Under warnings, you would have got
    Odd number of elements in hash assignment
    To solve your problem, you need a hash of arrays:
    my %chores = (Monday => ["dishes" , "1"], Tuesday => ["vacuum" , "2"], Wednesday => ["garbage" , "3"]); my $value = join ' ', @{ $chores{Wednesday} }; print "$value\n";
    See Perl Data Structures Cookbook and Perl references and nested data structures for details.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Keyed matrix
by daxim (Curate) on Sep 03, 2012 at 12:12 UTC
    A matrix is a two-dimensional ordered data structure. An array of arrayrefs is a simple way to represent this in Perl. Read perllol, perldsc. Hashes don't help you here.

    If you want to do calculation with matrices, use a CPAN module such as Math::GSL::Matrix.

Re: Keyed matrix
by aitap (Curate) on Sep 03, 2012 at 12:21 UTC

    What do you mean by "matrix"? There is no object in Perl (at least in the core modules) called "matrix". See perldata.

    What you actually need is a hash of arrays. Hashes can contain only (possibly blessed) scalar values, so you cannot just put arrays in the hash. Instead, need to create array references and put them in a hash. See perlreftut for more information on this. For example,

    my %hash = ( one => [ 1, 2, 3, ], two => [ 4, 5, 6, 7, ], three => [ 8, ], ); print ((join "\t", (sort keys %hash)),"\n"); my $i = 0; my $defined; do { $defined = 0; map {$defined++ if defined $hash{$_}->[$i]} (keys %hash); for (sort keys %hash) { print $hash{$_}->[$i] // " ", "\t"; }; print "\n"; $i++; } while ($defined > 0); __END__ one three two 1 8 4 2 5 3 6 7

    Sorry if my advice was wrong.
Re: Keyed matrix
by protist (Monk) on Sep 03, 2012 at 12:32 UTC

    You could do this using anonymous references...but you will have to dereference to get at the lists

    #!/usr/bin/perl use warnings; use strict; #create a hash where each key is matched with an anonymous array refer +ence my %chores = ( Monday => ["dishes","1"], Tuesday => ["vacuum","2"], Wednesday => ["garbage","3"]); #get the reference to the list stored in the key Wednesday my $valuereference = $chores{Wednesday}; #make value the list that valuereference is referring to my @value = @{$valuereference}; #print each element of the list @value, followed by a newline print $_."\n" for @value;

    I hope that helps :)

Re: Keyed matrix
by BillKSmith (Monsignor) on Sep 03, 2012 at 12:21 UTC

    Read the documentation about "hash of arrays" in perldoc perldsc (type perldoc perldsc at your command line). Let us know if you still have a problem. )

    Bill

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-19 16:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found