[32202] in Perl-Users-Digest
Perl-Users Digest, Issue: 3467 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Aug 8 00:09:24 2011
Date: Sun, 7 Aug 2011 21:09:06 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Sun, 7 Aug 2011 Volume: 11 Number: 3467
Today's topics:
Interfacing the advanced data structures <ela@yantai.org>
Re: Interfacing the advanced data structures <ben.usenet@bsb.me.uk>
Re: Interfacing the advanced data structures <ela@yantai.org>
Re: Interfacing the advanced data structures <tadmc@seesig.invalid>
Re: Interfacing the advanced data structures <jurgenex@hotmail.com>
Re: Interfacing the advanced data structures <ben.usenet@bsb.me.uk>
Re: Interfacing the advanced data structures <ben.usenet@bsb.me.uk>
Re: seeking advice on problem difficulty <ela@yantai.org>
Re: seeking advice on problem difficulty <tadmc@seesig.invalid>
Re: seeking advice on problem difficulty <ben.usenet@bsb.me.uk>
Re: seeking advice on problem difficulty <ela@yantai.org>
Re: seeking advice on problem difficulty <tadmc@seesig.invalid>
Spaces <remailer@reece.net.au>
Re: Spaces <tadmc@seesig.invalid>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sun, 7 Aug 2011 16:17:17 -0700
From: "ela" <ela@yantai.org>
Subject: Interfacing the advanced data structures
Message-Id: <j1le1r$163$1@ijustice.itsc.cuhk.edu.hk>
I still don't quite get "flattening" means and therefore post the codes.
While I just understood yesterday that @$group is good in array structures
but sparse indices, I don't know how to pass the corresponding data
structures to subroutine classify, as Tad McClellan pointed out passing
anything to subroutine makes a long list compressing everything together, so
where do the subrountines separate the items correspondingly?
#!/usr/bin/perl
use warnings;
use Switch;
use Data::Dumper;
use Statistics::Descriptive;
my $nl = "\n";
my ($conffile, $asmfile) = @ARGV;
my $outfile = $conffile . "_by_asm.xls";
open OUTFP, '>', $outfile or die "Can't open $outfile for writing: $!\n";
open my $r2fp, '<', $conffile or die "Can't open $conffile for reading:
$!\n";
#$conffile is a file with tab-delimited data like:
ID F9 F8 F7 F6 F5 F4 F3 F2 F1 Weight
1 hello dummy hi a bb oh SuperC1 C2 SubC1 0.5
1 hello dummy hi a bb oh SuperC1 C2 SubC3 0.5
2 ha dummy hi a bb oh SuperC1 C2 SubC3 1
3 ha dummy hi a bb oh SuperC1 C2 SubC4 1
...
my @column;
<$r2fp>;
while (<$r2fp>) {
chomp;
my (@row, $c) = split;
push @{$column[$c++]->[$row[0]]}, $_ foreach @row;
}
open( ASM, "<asm.file" );
while (<ASM>) {
if (/\{CTG/) { $getCtgID = 1; }
elsif (/iid:(\d+)/){if($getCtgID){$group=$1;$getCtgID=0; }}
elsif (/src:(\d+)/) {
if ($1 % 2 != 1) { $ID = $1 - 1;
} else { $ID = $1; } # print $1; <STDIN>;
push @$group, $ID;
}
}
}
sub flatten { map {@$_} @_ }
sub most_frequent
{
#print "MOST FREq", $most_freq; <STDIN>;
my ($most_freq, %count) = ('', '' => 0);
print "MOST FREq", $most_freq; <STDIN>;
for my $item (@_) {
$most_freq = $item if ++$count{$item} > $count{$most_freq};
}
return ($most_freq, $count{$most_freq}/@_);
}
sub classify
{
my ($group, $table, $threshold) = @_;
for (my $col = 9; $col > 6; $col--) {
my ($item, $freq) =
most_frequent(flatten(@{@$table[$col]}[@$group]));
return $item if $freq >= $threshold;
}
return 'inconsistent';
}
# most_frequent((1,2,3),(2,3,4));
# most_frequent([1,2,3],[2,3,4]);
# my ($item, $freq) = most_frequent(flatten([3,3,3],[2,3,3]));
# print "ITEM", $item, "FREQ", $freq;
classify(@group, @column, 0.7);
------------------------------
Date: Sun, 07 Aug 2011 22:38:37 +0100
From: Ben Bacarisse <ben.usenet@bsb.me.uk>
Subject: Re: Interfacing the advanced data structures
Message-Id: <0.e46e4af26e603597eae0.20110807223837BST.87k4aovr4y.fsf@bsb.me.uk>
"ela" <ela@yantai.org> writes:
> I still don't quite get "flattening" means and therefore post the
> codes.
Another phrase that might help: it means turning a nested structure into
one that is not nested anymore. An example being turning an array of
array references into a single array.
> While I just understood yesterday that @$group is good in array structures
> but sparse indices, I don't know how to pass the corresponding data
> structures to subroutine classify, as Tad McClellan pointed out passing
> anything to subroutine makes a long list compressing everything together, so
> where do the subrountines separate the items correspondingly?
I think you are trying to learn Perl without a good source. The
documentation is excellent, but you have to read a lot of text to get
the big picture so I prefer a good book.
Anyway, to pass separate arrays to function you must use references.
classify should be called like this:
classify(\@group, \@column, 0.7)
It then gets one single array argument (called @_) with three elements:
two array references and a number. Maybe I should have used more
helpful names. The arguments to classify end up in variables called:
sub classify
{
my ($group, $table, $threshold) = @_;
...
}
The first two are references which is why the arrays are referred to
using the @$group, @$table syntax. $group_ref and $tab_ref might have
been better. I don't know.
<snip>
> open( ASM, "<asm.file" );
> while (<ASM>) {
> if (/\{CTG/) { $getCtgID = 1; }
> elsif (/iid:(\d+)/){if($getCtgID){$group=$1;$getCtgID=0; }}
> elsif (/src:(\d+)/) {
> if ($1 % 2 != 1) { $ID = $1 - 1;
> } else { $ID = $1; } # print $1; <STDIN>;
> push @$group, $ID;
> }
> }
> }
This can't be what you tried since it has too many }s. It's important to
post actual code. I can't unravel what you are doing with $group. You
use it to store a number in one place and later use it as an array
reference. Note that if it is to be an array reference, then you call
classify like this:
classify($group, \@column, 0.7)
> sub flatten { map {@$_} @_ }
>
> sub most_frequent
> {
> #print "MOST FREq", $most_freq; <STDIN>;
> my ($most_freq, %count) = ('', '' => 0);
> print "MOST FREq", $most_freq; <STDIN>;
> for my $item (@_) {
> $most_freq = $item if ++$count{$item} > $count{$most_freq};
> }
> return ($most_freq, $count{$most_freq}/@_);
> }
>
>
> sub classify
> {
> my ($group, $table, $threshold) = @_;
> for (my $col = 9; $col > 6; $col--) {
Even though %table is an array reference, you can still use Perl's #
syntax to get the index of the last column:
for (my $col = $#$table - 1; $col > 6; $col--)
If this is part of a move complex program, I'd consider using the table
headings to populate a hash so I could map column names to indexes.
<snip>
> classify(@group, @column, 0.7);
I think you need
classify($group, \@column, 0.7);
here (and you need to do something with it's result) but if you correct
the earlier code to read the group into a plain array then you need
classify(\@group, \@column, 0.7);
--
Ben.
------------------------------
Date: Mon, 8 Aug 2011 11:12:04 -0700
From: "ela" <ela@yantai.org>
Subject: Re: Interfacing the advanced data structures
Message-Id: <j1nghi$po2$1@ijustice.itsc.cuhk.edu.hk>
"Ben Bacarisse" <ben.usenet@bsb.me.uk> wrote in message
news:0.e46e4af26e603597eae0.20110807223837BST.87k4aovr4y.fsf@bsb.me.uk...
> "ela" <ela@yantai.org> writes:
>
>> I still don't quite get "flattening" means and therefore post the
>> codes.
>
> Another phrase that might help: it means turning a nested structure into
> one that is not nested anymore. An example being turning an array of
> array references into a single array.
Thanks a lot. I finally get the meaning of "flattening".
>> open( ASM, "<asm.file" );
>> while (<ASM>) {
>> if (/\{CTG/) { $getCtgID = 1; }
>> elsif (/iid:(\d+)/){if($getCtgID){$group=$1;$getCtgID=0; }}
>> elsif (/src:(\d+)/) {
>> if ($1 % 2 != 1) { $ID = $1 - 1;
>> } else { $ID = $1; } # print $1; <STDIN>;
>> push @$group, $ID;
>> }
>> }
>> } <----------------
>
> This can't be what you tried since it has too many }s. It's important to
> post actual code. I can't unravel what you are doing with $group. You
> use it to store a number in one place and later use it as an array
> reference.
Yes, one more "}" (pointed by <--------------) was included. I removed other
irrelevant condition checking which does not affect the "group"
construction. In fact, I have to construct group by parsing a file that
contains keywords like "CTG" followed by "iid", which indicate the group
numbers, e.g. 1,2,3,4, ... 19
and then for each group, the member ID's are labeled with the keyword "src",
so I used the code
push @$group, $ID
to assign the members to the group they belong to.
>> sub classify
>> {
>> my ($group, $table, $threshold) = @_;
>> for (my $col = 9; $col > 6; $col--) {
>
> Even though %table is an array reference, you can still use Perl's #
> syntax to get the index of the last column:
>
> for (my $col = $#$table - 1; $col > 6; $col--)
>
> If this is part of a move complex program, I'd consider using the table
> headings to populate a hash so I could map column names to indexes.
Yes, you are brilliant to know that I have done that. Indeed I do something
like:
open my $r2fp, '<', $conffile or die "Can't open $conffile for reading:
$!\n";
my $line = <$r2fp>; chomp $line;
my @fields = split(/\t/, $line);
for (my $i=0; $i<@fields; $i++) { # print $fields[$i]; <STDIN>;
############## check fields ######
if ( $fields[$i] =~ /^Identity of query$/i) {
$qryii = $i; # print $qryii; <STDIN>;
} elsif ( $fields[$i] =~ /^Country$/i) { $fii = $i;
} elsif ( $fields[$i] =~ /^City$/i) { $gii = $i;
} elsif ( $fields[$i] =~ /^Street$/i){ $sii = $i;
}
}
while (<$r2fp>) {
chomp;
my (@row, $c) = split;
push @{$column[$c++]->[$row[0]]}, $_ foreach @row;
}
<snip>
@catchk = ($sii, $gii, $fii);
for $col (@catchk) {
my ($item, $freq) =
most_frequent(flatten(@{@$table[$col]}[@$group]));
return $item if $freq >= $threshold;
}
but now I start to realize that
"most_frequent(flatten(@{@$table[$col]}[@$group]));" may not be doing the
job I mean, likely due to my unclear problem specification.
I have 2 files to parse:
one file contains the group membership information and after parsing that
file, I should have something like:
grp 1: [1,19,22,387]
grp 2: [9,18,101]
grp 3: [2, 1119]
...
and another file that contains a large table that contains no group
membership information but only the ID's like:
1 hello blahblah ... UK London Downing 0.5
1 hello blahblah ... US New York Wall 0.5
2 hi ...
so the first two rows, having the ID "1", should belong to grp 1, the third
row, should belong to grp 3.
by using {@$table[$col]}[@$group], it appears to mean that by knowing the ID
can immediately also know the group number...
------------------------------
Date: Sun, 07 Aug 2011 21:36:20 -0500
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: Interfacing the advanced data structures
Message-Id: <slrnj3uiel.k5i.tadmc@tadbox.sbcglobal.net>
ela <ela@yantai.org> wrote:
>
> "Ben Bacarisse" <ben.usenet@bsb.me.uk> wrote in message
> news:0.e46e4af26e603597eae0.20110807223837BST.87k4aovr4y.fsf@bsb.me.uk...
>> "ela" <ela@yantai.org> writes:
>>
>>> I still don't quite get "flattening" means and therefore post the
>>> codes.
>>
>> Another phrase that might help: it means turning a nested structure into
>> one that is not nested anymore. An example being turning an array of
>> array references into a single array.
>
> Thanks a lot. I finally get the meaning of "flattening".
Ben's description needs repair if it is to be used for learning.
An example being turning an array of array references
into a single list.
See also:
perldoc -q difference
What is the difference between a list and an array?
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.
------------------------------
Date: Sun, 07 Aug 2011 20:17:06 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Interfacing the advanced data structures
Message-Id: <utku37d4aauckulbm7b4boo76qgauve2ci@4ax.com>
"ela" <ela@yantai.org> wrote:
>I still don't quite get "flattening" means and therefore post the codes.
>While I just understood yesterday that @$group is good in array structures
>but sparse indices, I don't know how to pass the corresponding data
>structures to subroutine classify, as Tad McClellan pointed out passing
>anything to subroutine makes a long list compressing everything together,
Perfect! So you do understand what flattening means because that is it.
Any list that is embedded in the argument list, is simply expanded until
the argument list contains only scalars. Example:
(1, (2, 3, 4), ((a, b), 5)) # the list contains only 3 elements!
is flattened into
(1, 2, 3, 4, a, b, 5) # now it contains 7 elements
> so
>where do the subrountines separate the items correspondingly?
They don't. Period.
If you want to pass complex data structures to a subroutine then you
have to pass references to those data structures.
jue
------------------------------
Date: Mon, 08 Aug 2011 04:18:32 +0100
From: Ben Bacarisse <ben.usenet@bsb.me.uk>
Subject: Re: Interfacing the advanced data structures
Message-Id: <0.33126b34a0cb61c421d2.20110808041832BST.87bow0vbef.fsf@bsb.me.uk>
"ela" <ela@yantai.org> writes:
> "Ben Bacarisse" <ben.usenet@bsb.me.uk> wrote in message
> news:0.e46e4af26e603597eae0.20110807223837BST.87k4aovr4y.fsf@bsb.me.uk...
>> "ela" <ela@yantai.org> writes:
<snip>
>>> open( ASM, "<asm.file" );
>>> while (<ASM>) {
>>> if (/\{CTG/) { $getCtgID = 1; }
>>> elsif (/iid:(\d+)/){if($getCtgID){$group=$1;$getCtgID=0; }}
>>> elsif (/src:(\d+)/) {
>>> if ($1 % 2 != 1) { $ID = $1 - 1;
>>> } else { $ID = $1; } # print $1; <STDIN>;
>>> push @$group, $ID;
>>> }
>>> }
>>> } <----------------
>>
>> This can't be what you tried since it has too many }s. It's important to
>> post actual code. I can't unravel what you are doing with $group. You
>> use it to store a number in one place and later use it as an array
>> reference.
>
> Yes, one more "}" (pointed by <--------------) was included. I removed other
> irrelevant condition checking which does not affect the "group"
> construction. In fact, I have to construct group by parsing a file that
> contains keywords like "CTG" followed by "iid", which indicate the group
> numbers, e.g. 1,2,3,4, ... 19
>
> and then for each group, the member ID's are labeled with the keyword "src",
> so I used the code
>
> push @$group, $ID
>
> to assign the members to the group they belong to.
Ah, that code does not do what you want then. Just use a plain array
for the groups. Each element of this array will itself be an array
(actually an array reference, of course).
When you see group number, set $group_id = $1;. Wehn you see a number
that is a member of the current group, you do
push @groups[$group_id], $1;
(there's no need to set $ID and then push $ID).
>>> sub classify
>>> {
>>> my ($group, $table, $threshold) = @_;
>>> for (my $col = 9; $col > 6; $col--) {
>>
>> Even though %table is an array reference, you can still use Perl's #
>> syntax to get the index of the last column:
>>
>> for (my $col = $#$table - 1; $col > 6; $col--)
>>
>> If this is part of a move complex program, I'd consider using the table
>> headings to populate a hash so I could map column names to indexes.
>
> Yes, you are brilliant to know that I have done that. Indeed I do something
> like:
>
> open my $r2fp, '<', $conffile or die "Can't open $conffile for reading:
> $!\n";
> my $line = <$r2fp>; chomp $line;
> my @fields = split(/\t/, $line);
> for (my $i=0; $i<@fields; $i++) { # print $fields[$i]; <STDIN>;
> ############## check fields ######
> if ( $fields[$i] =~ /^Identity of query$/i) {
> $qryii = $i; # print $qryii; <STDIN>;
> } elsif ( $fields[$i] =~ /^Country$/i) { $fii = $i;
> } elsif ( $fields[$i] =~ /^City$/i) { $gii = $i;
> } elsif ( $fields[$i] =~ /^Street$/i){ $sii = $i;
> }
> }
Actually that's not what I meant though it is similar. I think I'd find
those $fii, $gii and so on rather confusing but if it works for you...
> while (<$r2fp>) {
> chomp;
> my (@row, $c) = split;
> push @{$column[$c++]->[$row[0]]}, $_ foreach @row;
> }
>
> <snip>
>
> @catchk = ($sii, $gii, $fii);
> for $col (@catchk) {
> my ($item, $freq) =
> most_frequent(flatten(@{@$table[$col]}[@$group]));
> return $item if $freq >= $threshold;
> }
>
> but now I start to realize that
> "most_frequent(flatten(@{@$table[$col]}[@$group]));" may not be doing the
> job I mean, likely due to my unclear problem specification.
>
> I have 2 files to parse:
> one file contains the group membership information and after parsing that
> file, I should have something like:
>
> grp 1: [1,19,22,387]
> grp 2: [9,18,101]
> grp 3: [2, 1119]
> ...
>
> and another file that contains a large table that contains no group
> membership information but only the ID's like:
>
> 1 hello blahblah ... UK London Downing 0.5
> 1 hello blahblah ... US New York Wall 0.5
> 2 hi ...
>
> so the first two rows, having the ID "1", should belong to grp 1, the third
> row, should belong to grp 3.
You are right, I don't understand the problem anymore, but that does not
matter as long as you know what you need.
> by using {@$table[$col]}[@$group], it appears to mean that by knowing the ID
> can immediately also know the group number...
I don't see how it can mean that. It might be that I've been addressing
the wrong problem, but maybe now you have some more tools you can use to
solve the problem you actually have.
--
Ben.
------------------------------
Date: Mon, 08 Aug 2011 04:32:10 +0100
From: Ben Bacarisse <ben.usenet@bsb.me.uk>
Subject: Re: Interfacing the advanced data structures
Message-Id: <0.cce4263a26fc6d91865d.20110808043210BST.874o1svarp.fsf@bsb.me.uk>
Jürgen Exner <jurgenex@hotmail.com> writes:
> "ela" <ela@yantai.org> wrote:
>>I still don't quite get "flattening" means and therefore post the codes.
>>While I just understood yesterday that @$group is good in array structures
>>but sparse indices, I don't know how to pass the corresponding data
>>structures to subroutine classify, as Tad McClellan pointed out passing
>>anything to subroutine makes a long list compressing everything together,
>
> Perfect! So you do understand what flattening means because that is it.
>
> Any list that is embedded in the argument list, is simply expanded until
> the argument list contains only scalars. Example:
> (1, (2, 3, 4), ((a, b), 5)) # the list contains only 3 elements!
> is flattened into
> (1, 2, 3, 4, a, b, 5) # now it contains 7 elements
I should point out that I was using the term in the very general sense
of flattening data structures, because I needed to turn an array of
array references into a single list argument and, of course, Perl does
not do that automatically. The (for a beginner) slightly baffling code
map { @$_ } @_
uses Perl's list flattening to do this more general flattening.
Tad McClellan pointed out that I did not make the proper distinction
between lists and arrays, and he's quite right about that, but it was
deliberate. I was trying to avoid getting into it though I accept that
that might have turned out to be an unhelpful thing to have done.
<snip>
--
Ben.
------------------------------
Date: Sat, 6 Aug 2011 23:25:58 -0700
From: "ela" <ela@yantai.org>
Subject: Re: seeking advice on problem difficulty
Message-Id: <j1jipk$gec$1@ijustice.itsc.cuhk.edu.hk>
"Ben Bacarisse" <ben.usenet@bsb.me.uk> wrote in message
news:0.9023075ec05c58f409a6.20110805144133BST.87y5z8vuuq.fsf@bsb.me.uk...
> my @column;
> while (<>) {
> chomp;
> my (@row, $c) = split;
> push @{$column[$c++]->[$row[0]]}, $_ foreach @row;
> }
>
> The slice @{$column[$col]}[@some_array_of_rows] is now and array of
> array references so we need to flatten it. A function to do that is
>
> sub flatten { map {@$_} @_ }
>
> The classify function might then be
>
> sub classify
> {
> my ($group, $table, $threshold) = @_;
> for (my $col = 1; $col < $#column; $col++) {
> my ($item, $freq) =
> most_frequent(flatten(@{@$table[$col]}[@$group]));
> return $item if $freq >= $threshold;
> }
> return 'inconsistent';
> }
>
It seems that I have to spend at least 2 weeks to upgrade myself in using
advanced perl data structures by reading this:
http://docstore.mik.ua/orelly/perl3/prog/ch09_01.htm
Meanwhile, may I first ask what do you mean by "flatten" (@_ and {@$_} drive
me crazy...)? It seems that a scalar and a hash should be passed to function
most_frequent
sub most_frequent
{
my ($most_freq, %count) = ('', '' => 0);
------------------------------
Date: Sat, 06 Aug 2011 13:51:28 -0500
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: seeking advice on problem difficulty
Message-Id: <slrnj3r2r2.gh3.tadmc@tadbox.sbcglobal.net>
ela <ela@yantai.org> wrote:
> It seems that I have to spend at least 2 weeks to upgrade myself in using
> advanced perl data structures by reading this:
>
> [snip url]
Please do not post links to pirated copies of copyrighted material.
Please attempt to be an honorable person instead.
You can either buy a legal copy of Programming Perl,
or learn what you need from the standard documents that
come with perl:
perldoc perlreftut
perldoc perlref
perldoc perllol
perldoc perldsc
> Meanwhile, may I first ask what do you mean by "flatten" (@_ and {@$_} drive
> me crazy...)? It seems that a scalar and a hash should be passed to function
> most_frequent
If you have a question about subroutines in Perl, then you
should consult the documentation for Perl's subroutines:
perldoc perlsub
The 2nd paragraph addresses your question:
The Perl model for function call and return values is simple: all
functions are passed as parameters one single flat list of scalars, and
all functions likewise return to their caller one single flat list of
scalars. Any arrays or hashes in these call and return lists will
collapse, losing their identities
So, arguments to a subroutine are a list of scalars (and a hash is
not a scalar, so a hash cannot be passed).
> sub most_frequent
> {
> my ($most_freq, %count) = ('', '' => 0);
^^^^^^^^^^^^^
^^^^^^^^^^^^^
There is no hash on the RHS, only a list of 3 scalars.
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.
------------------------------
Date: Sat, 06 Aug 2011 23:40:19 +0100
From: Ben Bacarisse <ben.usenet@bsb.me.uk>
Subject: Re: seeking advice on problem difficulty
Message-Id: <0.ae5c79a4172c510b4373.20110806234019BST.871uwyw4do.fsf@bsb.me.uk>
"ela" <ela@yantai.org> writes:
> "Ben Bacarisse" <ben.usenet@bsb.me.uk> wrote in message
> news:0.9023075ec05c58f409a6.20110805144133BST.87y5z8vuuq.fsf@bsb.me.uk...
>> my @column;
>> while (<>) {
>> chomp;
>> my (@row, $c) = split;
>> push @{$column[$c++]->[$row[0]]}, $_ foreach @row;
>> }
>>
>> The slice @{$column[$col]}[@some_array_of_rows] is now and array of
>> array references so we need to flatten it.
This is a big part of the answer to this question:
<snip>
> Meanwhile, may I first ask what do you mean by "flatten" (@_ and {@$_} drive
> me crazy...)?
The columns are now made up of array references where before they
contained simple scalars. That's true also for any slice from a
column. In order for most_frequent to do it's job it must be passed all
the elements in all of the these referenced arrays. This conversion is
often described as flattening. The operation that does it
map { @$_ } @some_array_of_array_references
relies on a slightly odd property of Perl. @$_ is the array that $_ is
a reference to, and Perl joins multiple arrays together in an array
context like this. In almost all other languages I can think of some
sort of list or array append operation would have to be explicit in such
a flattening function, but in Perl it's implicit.
> It seems that a scalar and a hash should be passed to function
> most_frequent
>
> sub most_frequent
> {
> my ($most_freq, %count) = ('', '' => 0);
These are just local variables and tell you nothing about what should be
passed. The function goes on to process the argument array @_ treating
each element as a hash index for counting frequencies.
You'll know you've got the hang of it when you can explain the effects
of these three calls:
most_frequent((1,2,3),(2,3,4))
most_frequent([1,2,3],[2,3,4])
most_frequent(flatten([1,2,3],[2,3,4]))
--
Ben.
------------------------------
Date: Sun, 7 Aug 2011 08:03:57 -0700
From: "ela" <ela@yantai.org>
Subject: Re: seeking advice on problem difficulty
Message-Id: <j1kh4r$mft$1@ijustice.itsc.cuhk.edu.hk>
"Tad McClellan" <tadmc@seesig.invalid> wrote in message
news:slrnj3r2r2.gh3.tadmc@tadbox.sbcglobal.net...
> ela <ela@yantai.org> wrote:
>
>> It seems that I have to spend at least 2 weeks to upgrade myself in using
>> advanced perl data structures by reading this:
>>
>> [snip url]
>
>
> Please do not post links to pirated copies of copyrighted material.
>
> Please attempt to be an honorable person instead.
Instead of realizing the source to be pirated, I was too heavily relied on
"Google". I shall pay attention to that then.
>> sub most_frequent
>> {
>> my ($most_freq, %count) = ('', '' => 0);
> ^^^^^^^^^^^^^
> ^^^^^^^^^^^^^
>
> There is no hash on the RHS, only a list of 3 scalars.
the 1st scalar is '', and the 2nd is the key '' and the 3rd for value '0'?
------------------------------
Date: Sat, 06 Aug 2011 18:04:36 -0500
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: seeking advice on problem difficulty
Message-Id: <slrnj3rhlm.h2i.tadmc@tadbox.sbcglobal.net>
ela <ela@yantai.org> wrote:
>
> "Tad McClellan" <tadmc@seesig.invalid> wrote in message
> news:slrnj3r2r2.gh3.tadmc@tadbox.sbcglobal.net...
>> ela <ela@yantai.org> wrote:
>>
>>> It seems that I have to spend at least 2 weeks to upgrade myself in using
>>> advanced perl data structures by reading this:
>>>
>>> [snip url]
>>
>>
>> Please do not post links to pirated copies of copyrighted material.
>>
>> Please attempt to be an honorable person instead.
>
> Instead of realizing the source to be pirated, I was too heavily relied on
> "Google". I shall pay attention to that then.
A big tipoff was that the URL did not even spell oreilly correctly...
>>> sub most_frequent
>>> {
>>> my ($most_freq, %count) = ('', '' => 0);
>> ^^^^^^^^^^^^^
>> ^^^^^^^^^^^^^
>>
>> There is no hash on the RHS, only a list of 3 scalars.
>
> the 1st scalar is '', and the 2nd is the key '' and the 3rd for value '0'?
Exactly right.
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.
------------------------------
Date: 8 Aug 2011 01:23:46 -0000
From: Kulin Remailer <remailer@reece.net.au>
Subject: Spaces
Message-Id: <7B0TUWVX40763.3915046296@reece.net.au>
How does perl treat spaces? Does it ignore them as in Fortran?
------------------------------
Date: Sun, 07 Aug 2011 21:38:41 -0500
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: Spaces
Message-Id: <slrnj3uij1.k5i.tadmc@tadbox.sbcglobal.net>
Kulin Remailer <remailer@reece.net.au> wrote:
> How does perl treat spaces? Does it ignore them as in Fortran?
The second paragraph of
perldoc perlsyn
answers your question:
Perl is a free-form language, you can format and indent it however
you like. Whitespace mostly serves to separate tokens, unlike
languages like Python where it is an important part of the syntax.
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V11 Issue 3467
***************************************