Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I would like to get the first word from each row of my @AoA[][] and push it into the array @kmers. Can anybody help me get there? This is what I've done so far. Thanks in advance everybody!
my(@kmers);
my($x) = 0;
my($y) = 0;
for ($x=0, $x<7, $x++){
for ($y=0, $y<$AoA[$x], $y++){
my($firstword) = '';
$firstword = $AoA[$x][$y] =~ /^(\w+)/;
push @kmers, $firstword;
print $firstword."\n";
}
}
Re: First word
by hippo (Bishop) on Oct 21, 2016 at 11:17 UTC
|
#!/usr/bin/env perl
use strict;
use warnings;
my @AoA = (
['first word', 'greek latin'],
['alpha omega', 'beta test']
);
my @firsties;
for my $outer (@AoA) {
for my $inner (@$outer) {
push @firsties, $inner =~ /^(\w+)/;
}
}
print "@firsties\n";
| [reply] [d/l] |
|
/tmp>cat 1174444-mod.pl
#!/usr/bin/env perl
use strict;
use warnings;
my @AoA = (
['first word', 'greek latin'],
['alpha omega', 'beta test'],
["don't forget", "can't work", "won't fix" ],
["Kindergärten Kindergarten"]
);
my @firsties;
for my $outer (@AoA) {
for my $inner (@$outer) {
push @firsties, $inner =~ /^(\w+)/;
}
}
print "@firsties\n";
/tmp>perl 1174444-mod.pl
first greek alpha beta don can won Kinderg
/tmp>perl -v
This is perl 5, version 18, subversion 1 (v5.18.1) built for x86_64-li
+nux-thread-multi
Copyright 1987-2013, Larry Wall
Perl may be copied only under the terms of either the Artistic License
+ or the
GNU General Public License, which may be found in the Perl 5 source ki
+t.
Complete documentation for Perl, including FAQ lists, should be found
+on
this system using "man perl" or "perldoc perl". If you have access to
+ the
Internet, point your browser at http://www.perl.org/, the Perl Home Pa
+ge.
/tmp>
Alexander
--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
| [reply] [d/l] [select] |
|
$inner =~ /^(\w+)/
with:
$inner =~ /^(\S+)/
and we get:
print "@firsties\n";
#first greek alpha beta don't can't won't Kindergärten
Good point. \w means the characters that can be used within a Perl identifier [0-9_A-Za-z]
Sometimes, as in this case, \S (not a space) is useful. | [reply] [d/l] [select] |
|
my(@kmers);
for my$x (@AoA){
for my$y (@$x){
push @kmers, $y =~ /^(\w+)/;
print @kmers."\n";
}
}
but I am getting this output:
1
2
3
4
5
6
7
8
9
10
11
12
...
Is it printing just the scalar value? If so, why is that? I am often having this problem in other situations
| [reply] [d/l] |
|
| [reply] [d/l] |
|
Since you haven't provided an SSCCE it is rather hard to say with certainty but the suspicion has to be that your @AoA is incorrectly formed and may not even be an AoA at all. Perhaps it is a deeper structure but that's pure speculation since you have not shared it.
Update: GotToBTru looks to have found the likely reason. I can't imagine why Anonymonk changed that from my example.
| [reply] [d/l] |
|
#!/usr/bin/perl
use strict;
use warnings;
my @AoA = (
['firstRowCol1 asdf87534', 'firstRowCol2 junk lj6t90'],
['secondRowCol1 mnhibvygt7','secondRowCol2 7d7d5434']
);
my(@kmers);
for my $x (@AoA)
{
for my $y (@$x)
{
push @kmers, $y =~ /^(\w+)/; #first word of each element
print "@kmers\n";
}
}
__END__
In the above code,
print @kmers."\n"; #prints...
1
2
3
4
print @kmers,"\n"; #prints...
firstRowCol1
firstRowCol1firstRowCol2
firstRowCol1firstRowCol2secondRowCol1
firstRowCol1firstRowCol2secondRowCol1secondRowCol2
print "@kmers\n"; #prints...
firstRowCol1
firstRowCol1 firstRowCol2
firstRowCol1 firstRowCol2 secondRowCol1
firstRowCol1 firstRowCol2 secondRowCol1 secondRowCol2
Update: I noticed that you were using tab characters in the code. This is not a good idea because a number of problems arise. Not the least of which is that there is no standard definition of "how long a tab should be". In your program editor, set the option "convert tabs to spaces". That way the indentation will look the same to me as it does to you even though I'm using a different editor.
Another Update: As per the post from Hippo, there are certainly some folks who disagree with my opinion about tabs. I don't want to re-hash this, especially since this point was not a focus of the OP's original question. For those interested, read the thread comments and make up your own mind. | [reply] [d/l] [select] |
|
Re: First word
by Laurent_R (Canon) on Oct 21, 2016 at 11:13 UTC
|
One possible way to do it (demonstrated under the Perl debugger):
DB<1> @aoa = ([qw / 1 2 3 4/], [qw/5 6 7/], [qw/8 9/]);
DB<2> x \@aoa
0 ARRAY(0x302ef588)
0 ARRAY(0x3002a078)
0 1
1 2
2 3
3 4
1 ARRAY(0x302ef878)
0 5
1 6
2 7
2 ARRAY(0x302ef8c8)
0 8
1 9
DB<3> @result = map { $_->[0] } @aoa;
DB<4> x \@result
0 ARRAY(0x302ef848)
0 1
1 5
2 8
Update: If you prefer a for loop, you could replace line 3 (with the map) with the following:
push @result, $_->[0] for @aoa;
| [reply] [d/l] [select] |
|
|