Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > help on hash of hashes

Reply
Thread Tools

help on hash of hashes

 
 
Jay Tilton
Guest
Posts: n/a
 
      02-22-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (Stephen Moon) wrote:

: I didn't understand what he meant by "using printf without a format."
:
: What is a difference between using "print" and "printf"?

Consult perlfunc for explanations of each function's purpose.

: How would
: you use "printf" with a format? I am relatively new to perl. Maybe
: you can enlighten me. So, you are telling me that
:
: printf(DATA_OUT "$role=$HoH{$family}{$role}\n");
:
: should be written as
:
: printf(DATA_OUT "%s = %s\n", $role, $HoH{$family}{$role});

Yes, that is the appropriate printf() statement.

More simply, change the printf() to an ordinary print().

print( DATA_OUT "$role=$HoH{$family}{$role}\n" );

 
Reply With Quote
 
 
 
 
Stephen Moon
Guest
Posts: n/a
 
      02-23-2004
Anno,

> > --------------------------------------------------------
> > #!/usr/bin/perl
> > use strict; use warnings;
> >
> > my @columns = transpose( map [ split], <DATA>);
> >
> > print "@$_\n" for shift @columns; # print header
> > foreach ( @columns ) {
> > print join( ', ', @$_), "\n"; # print role columns
> > }
> >
> > sub transpose {
> > my $max = 0;
> > $max > @$_ or $max = @$_ for @_; # length of longest line
> > map [ map shift( @$_) || '', @_], 1 .. $max;
> > }


I have a hard time figuring out how transpose function. Maybe, a
little explanation? Especially where you have "map [ map shift( @$_)
|| '',@_], 1 .. $max" Furthermore, what would be difference in using
{} instead of [].

-Steve
> >
> > __DATA__
> > flintstones: lead=fred pal=barney
> > jetsons: lead=george wife=jane boy=elroy
> > simpsons: lead=homer wife=marge kid=bart

>
> cool! I will check it out! Well, the reason that I use sort is
> because I will have frequency index associated with magnitudes.
>
> Thanks for your help.
>
> -Steve

 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      02-23-2004
Stephen Moon <(E-Mail Removed)> wrote in comp.lang.perl.misc:

> Anno,


[...]

> > > sub transpose {
> > > my $max = 0;
> > > $max > @$_ or $max = @$_ for @_; # length of longest line
> > > map [ map shift( @$_) || '', @_], 1 .. $max;
> > > }

>
> I have a hard time figuring out how transpose function. Maybe, a
> little explanation? Especially where you have "map [ map shift( @$_)
> || '',@_], 1 .. $max"


Well, it is rather compressed, so let's make it more explicit. First let's
expand the outer map and introduce meaningful variable names and some
comments along the way. (I'll assume $max has been calculated as before.
Since it's the length of the longest line it is also the number of lines
in the transposed matrix).

my @lines = @_; # an array of arrays which represents a matrix
my @cols; # will become the transposed matrix
# extract $max columns
for ( 1 .. $max ) {
push @cols, [ map shift @$_ || '', @lines]; # unchanged
}
# done, return result
@cols;

Now do the same with the inner map. We are using a fundamental identity
here. If some expression EXPR returns a list there are two ways to
get an array reference to that list. You can write

$ref = [ EXPR ];

or you can do

my @array = LIST;
$ref = \ @array;

The original code uses the first way for each (anonymous) column. The
expanded code uses the second with the named variable @col. The result
is the same.

my @lines = @_; # an array of arrays which represents a matrix
my @cols; # will become the transposed matrix
# extract $max columns
for ( 1 .. $max ) {
# extract one column
my @col;
# walk through all lines
for my $line ( @lines ) {
# shave off one element of $line and add it to the current
# column. if there are no more elements left in the line, add
# an empty string. $line is destroyed in the process.
push @col, shift @$line || '';
}
# another column is ready, push it onto the result list
push @cols, \ @col;
}
# done, return result
@cols;

I hope that makes things clearer.

As presented, this transposition routine isn't quite a general-purpose
matrix transposition. For one, it's destructive. After using it on
a matrix, the matrix lines will be reduced to empty lists. A general-
purpose routine shouldn't do that. At least, a property like that must
be documented. In blinking red, with a howling background noise.

Secondly, it replaces undefined elements in the original matrix with
empty strings. (Sloppily, it would even replace a 0 element with an
empty string.) A general purpose routine has no business doing that,
but in the case at hand it was convenient.

> Furthermore, what would be difference in using
> {} instead of [].


"{}" produces a hashref from a list of pairs. "[]" produces an
arrayref from any list. The two are never[1] interchangeable.

Anno.

[1] Huh. I said "never" about a Perl construct. Someone is going to
come up with a counter-example.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
"Pseudo-hashes are deprecated" error and accessing a hash of hashes ernestm@mindspring.com Perl Misc 3 01-31-2006 04:40 AM
Hash of hashes, of hashes, of arrays of hashes Tim O'Donovan Perl Misc 5 10-28-2005 05:59 AM
Hashes of hashes or just one hash ? Perl Learner Perl Misc 11 06-09-2005 06:55 AM
Performance Improvement of complex data structure (hash of hashes of hashes) Scott Gilpin Perl Misc 2 08-26-2004 01:02 AM



Advertisments