sort results in ascending order
--_000_D225040F2D0F75448937B83D9527991A015480398Adenex1crick et_
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
I would like to print results in ascending order starting with $cell, $sect=
, and finally $carr.
I am getting the error:
Name "main::a" used only once: possible typo at ./DOband.pl line 6.
Name "main::b" used only once: possible typo at ./DOband.pl line 6.
Below is my code. Any help is greatly appreciated.
Thank you,
#!/usr/bin/perl
use warnings;
use strict;
sub num {$a <=3D> $b;}
while (<>)
{
chomp;
if (/;/)
{
my [at] data =3D split /;/;
my($cell,$sect,$carr,$RTD) =3D ($data[31],$data[32],$data[38],$data[26=
1]);
print "$cell\t $sect\t $carr\t $RTD\n";
}
}
Chris
--_000_D225040F2D0F75448937B83D9527991A015480398Adenex1crick et_--
Re: sort results in ascending order
On 11-03-16 09:26 AM, Chris Stinemetz wrote:
> I would like to print results in ascending order starting with $cell, $sect, and finally $carr.
You would need to store the data in a large array, then sort it.
# untested due lack of data
my [at] array = ();
while (<>)
{
chomp;
if (/;/)
{
my [at] data = split /;/;
my($cell,$sect,$carr,$RTD) =
($data[31],$data[32],$data[38],$data[261]);
push [at] array, {
cell => $cell,
sect => $sect,
carr => $carr,
RTD => $RTD,
};
}
}
my [at] sorted = map { $_->[0] }
sort {
$a->[1] <=> $b->[1]
|| $a->[2] <=> $b->[2]
|| $a->[3] <=> $b->[3]
}
map { [ $_, $_->{cell}, $_->{sect}, $_->{carr} ]}
[at] array;
--
Just my 0.00000002 million dollars worth,
Shawn
Confusion is the first step of understanding.
Programming is as much about organization and communication
as it is about coding.
The secret to great software: Fail early & often.
Eliminate software piracy: use only FLOSS.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
RE: sort results in ascending order
Thanks,
Where would I place print to see the results for validation?
On 11-03-16 09:26 AM, Chris Stinemetz wrote:
> I would like to print results in ascending order starting with $cell, $se=
ct, and finally $carr.
You would need to store the data in a large array, then sort it.
# untested due lack of data
my [at] array =3D ();
while (<>)
{
chomp;
if (/;/)
{
my [at] data =3D split /;/;
my($cell,$sect,$carr,$RTD) =3D
($data[31],$data[32],$data[38],$data[261]);
push [at] array, {
cell =3D> $cell,
sect =3D> $sect,
carr =3D> $carr,
RTD =3D> $RTD,
};
}
}
my [at] sorted =3D map { $_->[0] }
sort {
$a->[1] <=3D> $b->[1]
|| $a->[2] <=3D> $b->[2]
|| $a->[3] <=3D> $b->[3]
}
map { [ $_, $_->{cell}, $_->{sect}, $_->{carr} ]}
[at] array;
--
Just my 0.00000002 million dollars worth,
Shawn
Confusion is the first step of understanding.
Programming is as much about organization and communication
as it is about coding.
The secret to great software: Fail early & often.
Eliminate software piracy: use only FLOSS.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: sort results in ascending order
On 11-03-16 10:58 AM, Chris Stinemetz wrote:
> Where would I place print to see the results for validation?
Since [at] sorted contains the sorted data, anywhere after it gets the
sorted data.
--
Just my 0.00000002 million dollars worth,
Shawn
Confusion is the first step of understanding.
Programming is as much about organization and communication
as it is about coding.
The secret to great software: Fail early & often.
Eliminate software piracy: use only FLOSS.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
RE: sort results in ascending order
Good day,
Your help is greatly appreciated. Thanks
For some reason I am not getting the sorted list in my output file. Instead=
I am getting the following:
bc8) HASH(0x100d0d78) HASH(0x100d15e8) HASH(0x100d0f28) HASH(0x100d0c58) HA=
SH(0x100d1168) HASH(0x100d1678) HASH(0x100d21
70) HASH(0x100d0ce8) HASH(0x100b4450) HASH(0x100d2200) HASH(0x100b3eb0) HAS=
H(0x100d1438) HASH(0x100d0e98) HASH(0x100d205
0) HASH(0x100b47b0) HASH(0x100d10d8) HASH(0x100b4720) HASH(0x100d1288) HASH=
(0x100d1048) HASH(0x100d0e08) HASH(0x100b42a0
) HASH(0x100b4690) HASH(0x100d13a8) HASH(0x100d1318) HASH(0x100b4180) HASH(=
0x100b4210) HASH(0x100d1558) HASH(0x100b4060)
HASH(0x100b4600) HASH(0x100b3d90) HASH(0x100d0b38) HASH(0x100d0fb8) HASH(0=
x100b43c0) HASH(0x100b3e20) HASH(0x100b40f0)
HASH(0x100d20e0) HASH(0x100d0a18) HASH(0x100d14c8) HASH(0x100b4330):
#!/usr/bin/perl
use warnings;
use strict;
my $filepath =3D "test.txt";
my $outfile =3D "output.txt";
open (OUTFILE, ">> $outfile") || die "ERROR: opening $filepath\n";
my [at] array =3D ();
#sub num {$a <=3D> $b;}
while (<>)
{
chomp;
if (/;/)
{
my [at] data =3D split /;/;
my($cell,$sect,$carr,$RTD) =3D ($data[31],$data[32],$data[38],$data[261])=
;
push [at] array, {
cell =3D> $cell,
sect =3D> $sect,
carr =3D> $carr,
RTD =3D> $RTD,
};
}
}
my [at] sorted =3D map { $_->[0] }
sort {
$a->[1] <=3D> $b->[1]
|| $a->[2] <=3D> $b->[2]
|| $a->[3] <=3D> $b->[3]
}
map { [ $_, $_->{cell}, $_->{sect}, $_->{carr} ]}
[at] array;
print OUTFILE " [at] sorted:\n";
=09
close OUTFILE;
#RTD 1 unit =3D 4 chips RUM Field 16 0 - 2^16 - 1 milliseconds
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: sort results in ascending order
On 11-03-17 11:04 AM, Chris Stinemetz wrote:
> print OUTFILE " [at] sorted:\n";
for my $tuple ( [at] sorted ){
print "$tuple->{cell} $tuple->{sect} $tuple->{carr} $tuple->{RTD}\n";
}
--
Just my 0.00000002 million dollars worth,
Shawn
Confusion is the first step of understanding.
Programming is as much about organization and communication
as it is about coding.
The secret to great software: Fail early & often.
Eliminate software piracy: use only FLOSS.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: sort results in ascending order
On Thu, Mar 17, 2011 at 11:04, Chris Stinemetz
<cstinemetz [at] cricketcommunications.com> wrote:
snip
> For some reason I am not getting the sorted list in my output file. Inste=
ad I am getting the following:
>
> bc8) HASH(0x100d0d78) HASH(0x100d15e8) HASH(0x100d0f28) HASH(0x100d0c58) =
HASH(0x100d1168) HASH(0x100d1678)
snip
You are getting these results because the array holds hash references.
snip
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0push [at] array, {
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0cell =3D> $cell,
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0sect =3D> $sect,
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0carr =3D> $carr,
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0RTD =C2=A0=3D> $RTD,
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0};
snip
This line of code pushes a hash reference onto the array
snip
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0print OUTFILE " [at] so=
rted:\n";
snip
And this line prints out the whole array. To get at the individual
elements of the hash, you must ask for them individually:
for my $hashref ( [at] sorted) {
print "$hashref->{cell} $hashref->{sect} $hashref->{carr}
$hashref->{RTD}\n";
}
Or all together with a hash slice:
for my $hashref ( [at] sorted) {
print " [at] {$hashref}{qw/cell sect carr RTD/}\n";
}
You may find the documentation on references helpful:
http://perldoc.perl.org/perlreftut.html
http://perldoc.perl.org/perlref.html
http://perldoc.perl.org/perldsc.html
or
perldoc perlreftut
perldoc perlref
perldoc perldsc
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: sort results in ascending order
On 16/03/2011 14:58, Chris Stinemetz wrote:
> Shawn H Corey wrote:
>> On 11-03-16 09:26 AM, Chris Stinemetz wrote:
>>>
>>> I would like to print results in ascending order starting with $cell, $sect, and finally $carr.
>>
>> You would need to store the data in a large array, then sort it.
>>
>> # untested due lack of data
>> my [at] array = ();
>>
>> while (<>)
>> {
>> chomp;
>> if (/;/)
>> {
>> my [at] data = split /;/;
>> my($cell,$sect,$carr,$RTD) = ($data[31],$data[32],$data[38],$data[261]);
>> push [at] array, {
>> cell => $cell,
>> sect => $sect,
>> carr => $carr,
>> RTD => $RTD,
>> };
>> }
>> }
>>
>> my [at] sorted = map { $_->[0] }
>> sort {
>> $a->[1]<=> $b->[1]
>> || $a->[2]<=> $b->[2]
>> || $a->[3]<=> $b->[3]
>> }
>> map { [ $_, $_->{cell}, $_->{sect}, $_->{carr} ]}
>> [at] array;
>
> Where would I place print to see the results for validation?
If you are uncomfortable with arrays of hash references, it may be
better for you to create an array of records containing just the four
fields you are interested in, and then sort that. Such a program is
shown below. It is tested and seems to work fine.
This method is inefficient, as the records are split every time sort
needs to compare a pair of them, but unless you are dealing with vast
amounts of data this will not be a problem. It is very important that
you understand the code you write, so that you can answer questions on
it and maintain it.
HTH,
Rob
use strict;
use warnings;
my [at] array;
while (<DATA>) {
next unless /;/;
chomp;
my [at] data = ( split /;/ )[31,32,38,261];
push [at] array, join ';', [at] data;
}
[at] array = sort {
my [at] aa = split /;/, $a;
my [at] bb = split /;/, $b;
$aa[0] <=> $bb[0] or
$aa[1] <=> $bb[1] or
$aa[2] <=> $bb[2];
} [at] array;
print "$_\n" foreach [at] array;
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: sort results in ascending order
On 11-03-17 01:05 PM, Rob Dixon wrote:
> If you are uncomfortable with arrays of hash references
Sooner or later every Perl programmer has to become comfortable with
complex data structures. Why not start now?
--
Just my 0.00000002 million dollars worth,
Shawn
Confusion is the first step of understanding.
Programming is as much about organization and communication
as it is about coding.
The secret to great software: Fail early & often.
Eliminate software piracy: use only FLOSS.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: sort results in ascending order
On 17/03/2011 17:22, Chris Stinemetz wrote:
> From: Rob Dixon [mailto:rob.dixon [at] gmx.com]
>>
>> If you are uncomfortable with arrays of hash references, it may be
>> better for you to create an array of records containing just the four
>> fields you are interested in, and then sort that. Such a program is
>> shown below. It is tested and seems to work fine.
>>
>> This method is inefficient, as the records are split every time sort
>> needs to compare a pair of them, but unless you are dealing with vast
>> amounts of data this will not be a problem. It is very important that
>> you understand the code you write, so that you can answer questions on
>> it and maintain it.
>>
>> HTH,
>>
>> Rob
>>
>> use strict;
>> use warnings;
>>
>> my [at] array;
>>
>> while (<DATA>) {
>> next unless /;/;
>> chomp;
>> my [at] data = ( split /;/ )[31,32,38,261];
>> push [at] array, join ';', [at] data;
>> }
>>
>> [at] array = sort {
>> my [at] aa = split /;/, $a;
>> my [at] bb = split /;/, $b;
>> $aa[0]<=> $bb[0] or
>> $aa[1]<=> $bb[1] or
>> $aa[2]<=> $bb[2];
>> } [at] array;
>>
>> print "$_\n" foreach [at] array;
>
> Thanks Rob,
>
> I like you approach. Just curious how can I change the output
> results to be tab separated and stead of ; separated. Below is what
> the currentoutput is like with your code:
>
> 837;2;975;147
> 837;2;1025;
> 837;2;1025;163
> 837;3;975;174
> 837;3;975;
> 837;3;1025;25
> 837;3;1025;65
> 840;1;1025;
> 840;2;975;
> 841;2;975;149
> 841;2;975;
> 842;1;975;
> 848;2;975;
> 850;1;975;
> 850;3;975;
(Please bottom-post your responses to this list, so that extended
threads remain comprehensible. Thanks.)
I would hope that you could solve that problem for yourself Chris?
At the end of the while loop the four fields are joined with semicolons
to form the new records, so the join string needs to be changed to a tab
character "\t".
Within the sort block, these records are split on semicolons to
determine the sort order. The split regex needs to be changed to match
tabs instead /\t/.
Cheers,
Rob
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: sort results in ascending order
On 17/03/2011 17:22, Shawn H Corey wrote:
> On 11-03-17 01:05 PM, Rob Dixon wrote:
>>
>> If you are uncomfortable with arrays of hash references
>
> Sooner or later every Perl programmer has to become comfortable with
> complex data structures. Why not start now?
A beginners list isn't the place to introduce arbitrarily complex Perl
constructs. Replies have to be sensitive to the ability of the OP or
they may confuse instead of help. I judged that Chris may well have
trouble with anonymous hashes and Schwartzian transforms and, as I
wrote, I believe it is important that software should be completely
understood by its author.
Rob
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: sort results in ascending order
On 11-03-17 02:04 PM, Rob Dixon wrote:
> A beginners list isn't the place to introduce arbitrarily complex Perl
> constructs. Replies have to be sensitive to the ability of the OP or
> they may co
On the other hand, telling them to use a kluge to get the results they
want is very bad advice. Tell them to do it right from the start.
If you want to tack the data together into a string, do it right:
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
# Make Data::Dumper pretty
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Indent = 1;
# Set maximum depth for Data::Dumper, zero means unlimited
local $Data::Dumper::Maxdepth = 0;
# TBD
sub encode_tsv {
my [at] data = [at] _;
my %encodings = (
"\t" => "\\t",
"\\" => "\\\\",
);
for my $datum ( [at] data ){
$datum =~ s{ ( [\\\t] ) }{$encodings{$1}}gmsx;
}
return join( "\t", [at] data );
}
sub decode_tsv {
my $str = shift [at] _;
my %decodings = (
"t" => "\t",
"\\" => "\\",
);
my [at] data = split( "\t", $str );
for my $datum ( [at] data ){
$datum =~ s{ \\ ( . ) }{$decodings{$1}}gmsx;
}
return [at] data;
}
sub show {
my [at] data = [at] _;
print Dumper \ [at] data;
my $str = encode_tsv( [at] data );
print Dumper $str;
[at] data = decode_tsv( $str );
print Dumper \ [at] data;
}
print "Test 1:\n";
show( qw( fee fie foo fum ));
print "\n\nTest 1:\n";
show(
"string\twith\ttabs",
"string\\with\\backslashes",
);
--
Just my 0.00000002 million dollars worth,
Shawn
Confusion is the first step of understanding.
Programming is as much about organization and communication
as it is about coding.
The secret to great software: Fail early & often.
Eliminate software piracy: use only FLOSS.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: sort results in ascending order
On 17/03/2011 18:43, Shawn H Corey wrote:
> On 11-03-17 02:04 PM, Rob Dixon wrote:
>>
>> A beginners list isn't the place to introduce arbitrarily complex Perl
>> constructs. Replies have to be sensitive to the ability of the OP or
>> they may co
>
> On the other hand, telling them to use a kluge to get the results they
> want is very bad advice. Tell them to do it right from the start.
>
> If you want to tack the data together into a string, do it right:
[snip sixty lines of code]
We have established that Chris's data is numeric. Your code is overkill.
I disagree with you. Let's leave it that way.
Rob
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: sort results in ascending order
On 11-03-17 03:23 PM, Rob Dixon wrote:
> We have established that Chris's data is numeric. Your code is overkill.
No, it's not. Just because this case does not have the join character
in the data, does mean it will never happen. What the novice programmer
is going to do is decide that this technique is simpler than using a
"complex" data structure and use it all the time. That is, until one
day it breaks...
If you're going to teach a technique, teach the whole thing. Anything
less is a disservice.
--
Just my 0.00000002 million dollars worth,
Shawn
Confusion is the first step of understanding.
Programming is as much about organization and communication
as it is about coding.
The secret to great software: Fail early & often.
Eliminate software piracy: use only FLOSS.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: sort results in ascending order
>>>>> "RD" == Rob Dixon <rob.dixon [at] gmx.com> writes:
RD> On 17/03/2011 17:22, Shawn H Corey wrote:
>> On 11-03-17 01:05 PM, Rob Dixon wrote:
>>>
>>> If you are uncomfortable with arrays of hash references
>>
>> Sooner or later every Perl programmer has to become comfortable with
>> complex data structures. Why not start now?
RD> A beginners list isn't the place to introduce arbitrarily complex Perl
RD> constructs. Replies have to be sensitive to the ability of the OP or
RD> they may confuse instead of help. I judged that Chris may well have
RD> trouble with anonymous hashes and Schwartzian transforms and, as I
RD> wrote, I believe it is important that software should be completely
RD> understood by its author.
you can also point out Sort::Maker which can do complex sorts with much
less coding and effort. if you can extract a key and describe how it
sorts, you can use the module. a solid beginner should be able to use
it.
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/