Sorting Records
am 20.11.2005 06:18:16 von flyaway840
How do you sort a record that has 6 items in the record, with lots
of these records in a file, and you want to sort a number field in
that record file?
I have read all the sort etc. in perldoc and they did not help, so
please don't just say see perldoc sort etc. That will not do me any
good. Please show me with example code.
You have a record with
A
B
C
D
E
F
Items as the record, and lots of these records, but you want
to sort these records by the item "E". With probably rnkeysort
or something, how do you pick out that item, "E" in a record to
do the sorting by?
Thanks a lot.
Re: Sorting Records
am 20.11.2005 10:02:44 von Gunnar Hjalmarsson
flyaway840@hotmail.com wrote:
> How do you sort a record that has 6 items in the record, with lots
> of these records in a file, and you want to sort a number field in
> that record file?
>
> I have read all the sort etc. in perldoc and they did not help, so
> please don't just say see perldoc sort etc. That will not do me any
> good. Please show me with example code.
Then you have a reading comprehension problem.
perldoc -f sort
includes example code that does just that.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Re: Sorting Records
am 20.11.2005 10:12:05 von Joe Smith
flyaway840@hotmail.com wrote:
> How do you sort a record that has 6 items in the record, with lots
> of these records in a file, and you want to sort a number field in
> that record file?
Sort::Array - This extended sorting algorithm allows you to
sort an array by ANY field number, not only the first.
and
http://en.wikipedia.org/wiki/Schwartzian_transform
-Joe
Re: Sorting Records
am 21.11.2005 16:29:44 von Paul Lalli
flyaway840@hotmail.com wrote:
> How do you sort a record that has 6 items in the record, with lots
> of these records in a file, and you want to sort a number field in
> that record file?
Split that record into a list. Write your sort subroutine to look at
the Nth field of that list. Probably use a Schwartzian transform to
make it more efficient.
> I have read all the sort etc. in perldoc and they did not help, so
> please don't just say see perldoc sort etc. That will not do me any
> good. Please show me with example code.
Perhaps you could explain how the examples in perldoc sort did not meet
your qualifications?
> You have a record with
>
> A
> B
> C
> D
> E
> F
>
> Items as the record, and lots of these records, but you want
> to sort these records by the item "E". With probably rnkeysort
> or something, how do you pick out that item, "E" in a record to
> do the sorting by?
None of this has any meaning. Show us with *real* data. Provide
example sample input, and the corresponding output you want to achieve
from it.
#!/usr/bin/perl
use strict;
use warnings;
#Using the Schwartzian transfer, create the
#anonymous array ref to hold the fourth field
#of each line, then compare on those fields
my @sorted = map { $_->[0] }
sort { $a->[1] <=> $b->[1] }
map { [$_, (split /\|/)[3]] } ;
print "Sorted:\n", @sorted, "\n";
__DATA__
foo|bar|baz|20|apple
hello|world|Itty|10|banana
quick|brown|fox|15|grapes
over|lazy|dog|12.5|orange
Sorted:
hello|world|Itty|10|banana
over|lazy|dog|12.5|orange
quick|brown|fox|15|grapes
foo|bar|baz|20|apple
If this isn't what you're looking for, again, provide *actual* sample
input and corresponding desired output.
Paul Lalli
Re: Sorting Records
am 22.11.2005 04:53:13 von flyaway840
Paul Lalli wrote:
> flyaway840@hotmail.com wrote:
> > How do you sort a record that has 6 items in the record, with lots
> > of these records in a file, and you want to sort a number field in
> > that record file?
>
> Split that record into a list. Write your sort subroutine to look at
> the Nth field of that list. Probably use a Schwartzian transform to
> make it more efficient.
Had I not read 7 sites on this subject the above would make
a beginner clueless. I thank you two, Joe Smith, and you.
>
> > I have read all the sort etc. in perldoc and they did not help, so
> > please don't just say see perldoc sort etc. That will not do me any
> > good. Please show me with example code.
>
> Perhaps you could explain how the examples in perldoc sort did not meet
> your qualifications?
Aparently most of you are unable to understand the concept
of a beginner. :-) If you say go see perldoc sort we are clueless
what that is. If we knew what that was, we probably would not
be here. I had no idea that -f changes what you see if you put
in perldoc -f sort you get all new information. Still, it is more
helpful on this subject, but still does not tell you enough to
actually do the coding. A more helpful way is as below the
actual code. But I suspect it will not work, as is, for me, because
I have tried a similar coding. I have used as the example at:
http://evolt.org/node/758
with help at:
http://www.searchengineforums.com/apps/webmaster.forums/acti on::thread/thread::1076701811/forum::php/
http://www.stonehenge.com/merlyn/UnixReview/col06.html
Which I get a list of unsorted records as they were, and
and the records end up looking like (unsorted):
Name
,
,
,
,
address
,
,
,
,
etc.
Not:
Name
address
ect.
In the sorted form.
>
> > You have a record with
> >
> > A
> > B
> > C
> > D
> > E
> > F
> >
> > Items as the record, and lots of these records, but you want
> > to sort these records by the item "E". With probably rnkeysort
> > or something, how do you pick out that item, "E" in a record to
> > do the sorting by?
>
> None of this has any meaning. Show us with *real* data. Provide
> example sample input, and the corresponding output you want to achieve
> from it.
1000033 (internal code)
title
discription
pictureURL
165 (ranking number)
30 (item number)
And I want to sort the file by the "ranking number", largest number
on top of the record file, keeping all the other information together
with this record, in the sort. The file name this information comes
from is "temp.txt". The above information goes to "temp.txt" then is
to be sorted, then the "temp.txt" file is to be renamed to the original
file
name to replace it with the sorted records. Which makes sure the
highest ranking number is always on top of the web page.
>
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> #Using the Schwartzian transfer, create the
> #anonymous array ref to hold the fourth field
> #of each line, then compare on those fields
> my @sorted = map { $_->[0] }
> sort { $a->[1] <=> $b->[1] }
> map { [$_, (split /\|/)[3]] } ;
> print "Sorted:\n", @sorted, "\n";
>
I will give this a try, but I don't know if split is needed, because
the record is as above:
A
B
C
D
not A B C D on one line in the file, as below.
> __DATA__
> foo|bar|baz|20|apple
> hello|world|Itty|10|banana
> quick|brown|fox|15|grapes
> over|lazy|dog|12.5|orange
>
> Sorted:
> hello|world|Itty|10|banana
> over|lazy|dog|12.5|orange
> quick|brown|fox|15|grapes
> foo|bar|baz|20|apple
>
>
> If this isn't what you're looking for, again, provide *actual* sample
> input and corresponding desired output.
>
> Paul Lalli
Thanks
Re: Sorting Records
am 22.11.2005 14:17:20 von Paul Lalli
flyaway840@hotmail.com wrote:
> Paul Lalli wrote:
> > flyaway840@hotmail.com wrote:
> >
> > Split that record into a list. Write your sort subroutine to look at
> > the Nth field of that list. Probably use a Schwartzian transform to
> > make it more efficient.
>
> Had I not read 7 sites on this subject the above would make
> a beginner clueless. I thank you two, Joe Smith, and you.
A beginner is expected to both ask questions and do research on his/her
own for anything he/she does not understand. I had (and continue to
have) no intention of explaining every detail to anyone. It is
absolutely correct that you should have read "7 sites" on the subject.
> > > I have read all the sort etc. in perldoc and they did not help, so
> > > please don't just say see perldoc sort etc. That will not do me any
> > > good. Please show me with example code.
> >
> > Perhaps you could explain how the examples in perldoc sort did not meet
> > your qualifications?
>
> Aparently most of you are unable to understand the concept
> of a beginner. :-)
I will never understand this. Do you honestly think I, or anyone else
you consider not a beginner, was born with the knowledge I have? That
I was never a beginner myself? I learned the same way I encourage
anyone else to learn - ask questions, read documentation, try writing
your own code.
> If you say go see perldoc sort we are clueless
> what that is.
So what is preventing you from asking what perldoc is, or from even
doing a Google search for perldoc?
> If we knew what that was, we probably would not
> be here. I had no idea that -f changes what you see if you put
> in perldoc -f sort you get all new information.
Then I encourage you to read the documentation for perldoc itself:
man perldoc
or
perldoc perldoc
> Still, it is more
> helpful on this subject, but still does not tell you enough to
> actually do the coding. A more helpful way is as below the
> actual code.
I suspect we have different definitions of "helpful". perldoc -f sort
tells you *exactly* how sort() works. Getting it to do what you want
is, yes, left as an exercise for the reader.
> Which I get a list of unsorted records as they were, and
> and the records end up looking like (unsorted):
>
> Name
> ,
> ,
> ,
> ,
> address
> ,
> ,
> ,
> ,
> etc.
>
>
> Not:
>
> Name
> address
> ect.
>
> In the sorted form.
>
I have no idea what any of this means. Post real examples, real code,
real input and real output.
> > Show us with *real* data. Provide
> > example sample input, and the corresponding output you want to achieve
> > from it.
>
>
> 1000033 (internal code)
> title
> discription
> pictureURL
> 165 (ranking number)
> 30 (item number)
>
> And I want to sort the file by the "ranking number", largest number
> on top of the record file, keeping all the other information together
> with this record, in the sort.
You still haven't shown *real* data. You've shown exactly one record.
What delimits records in the file? How do you know where one record
ends and another begins? Your either making assumptions, or asking us
to.
> The file name this information comes
> from is "temp.txt". The above information goes to "temp.txt" then is
> to be sorted, then the "temp.txt" file is to be renamed to the original
> file
> name to replace it with the sorted records. Which makes sure the
> highest ranking number is always on top of the web page.
I have difficulty parsing any of this. Again, *real* data, speaking
Perl rather than English, would be much more clear and helpful.
> > #!/usr/bin/perl
> > use strict;
> > use warnings;
> >
> > #Using the Schwartzian transfer, create the
> > #anonymous array ref to hold the fourth field
> > #of each line, then compare on those fields
> > my @sorted = map { $_->[0] }
> > sort { $a->[1] <=> $b->[1] }
> > map { [$_, (split /\|/)[3]] } ;
> > print "Sorted:\n", @sorted, "\n";
> >
>
>
> I will give this a try, but I don't know if split is needed, because
> the record is as above:
>
> A
> B
> C
> D
>
> not A B C D on one line in the file, as below.
Okay. I will make an assumption, then, that your "records" are simply
sequences of 6 lines, where the fifth line contains the data on which
you want to base your sort.
#!/usr/bin/perl
use strict;
use warnings;
my @records;
my $curline;
#read each line
while (){
$curline .= $_;
#every sixth line, store a record
if ($. % 6 == 0) {
push @records, $curline;
undef $curline;
}
}
#now we have an array of the records.
my @sorted = map { $_->[0] }
sort { $b->[1] <=> $a->[1] }
map { [ $_, (split /\n/)[4] ] } @records;
print @sorted;
__DATA__
1000033
title
discription
pictureURL
165
30
1000034
title
discription
pictureURL
160
30
1000035
title
discription
pictureURL
170
30
Output:
1000035
title
discription
pictureURL
170
30
1000033
title
discription
pictureURL
165
30
1000034
title
discription
pictureURL
160
30
Reading from the file, printing to a new file, and renaming the files
are all left as exercises for you to work on. Read the documentation
(see below), and then ask if you have problems getting it to work:
perldoc -f open
perldoc perlopentut
perldoc -f print
perldoc -f rename
Paul Lalli
Re: Sorting Records
am 23.11.2005 06:18:24 von flyaway840
Paul Lalli wrote:
> flyaway840@hotmail.com wrote:
> > Paul Lalli wrote:
> > > flyaway840@hotmail.com wrote:
> > >
> > > Split that record into a list. Write your sort subroutine to look at
> > > the Nth field of that list. Probably use a Schwartzian transform to
> > > make it more efficient.
> >
> > Had I not read 7 sites on this subject the above would make
> > a beginner clueless. I thank you two, Joe Smith, and you.
>
> A beginner is expected to both ask questions and do research on his/her
> own for anything he/she does not understand. I had (and continue to
> have) no intention of explaining every detail to anyone. It is
> absolutely correct that you should have read "7 sites" on the subject.
>
> > > > I have read all the sort etc. in perldoc and they did not help, so
> > > > please don't just say see perldoc sort etc. That will not do me any
> > > > good. Please show me with example code.
> > >
> > > Perhaps you could explain how the examples in perldoc sort did not meet
> > > your qualifications?
> >
> > Aparently most of you are unable to understand the concept
> > of a beginner. :-)
>
> I will never understand this. Do you honestly think I, or anyone else
> you consider not a beginner, was born with the knowledge I have? That
> I was never a beginner myself? I learned the same way I encourage
> anyone else to learn - ask questions, read documentation, try writing
> your own code.
I don't know what it is about perl groups or physics groups, but
if you do ask a question usually you get an answer as, what are
you some kind of retard. With expecting you to have all the
information prior to asking the question. As: "Then you have a
reading comprehension problem. perldoc -f sort includes
example code that does just that." So we were supposed to
have known -f does soemthing we did not know in the first
place. If it pisses those off for us to ask questions, or to ask to
find the document to read, how are we supposed to ask those
for it? When someone says please don't just say see
perldoc whatever that means they have tried to do it on their
own and need help. Calling us retards is not help, it is arrogance.
Thanks for the code below, I will give it a try. And your assumption
was correct, data is on a single line, only the count of 6 X 6 ...
separates them. You know one record from the other by the
count. If it is line 7 it is the next record. As in sequential cobal
files.
Thanks
>
> > If you say go see perldoc sort we are clueless
> > what that is.
>
> So what is preventing you from asking what perldoc is, or from even
> doing a Google search for perldoc?
>
> > If we knew what that was, we probably would not
> > be here. I had no idea that -f changes what you see if you put
> > in perldoc -f sort you get all new information.
>
> Then I encourage you to read the documentation for perldoc itself:
> man perldoc
> or
> perldoc perldoc
>
> > Still, it is more
> > helpful on this subject, but still does not tell you enough to
> > actually do the coding. A more helpful way is as below the
> > actual code.
>
> I suspect we have different definitions of "helpful". perldoc -f sort
> tells you *exactly* how sort() works. Getting it to do what you want
> is, yes, left as an exercise for the reader.
>
> > Which I get a list of unsorted records as they were, and
> > and the records end up looking like (unsorted):
> >
> > Name
> > ,
> > ,
> > ,
> > ,
> > address
> > ,
> > ,
> > ,
> > ,
> > etc.
> >
> >
> > Not:
> >
> > Name
> > address
> > ect.
> >
> > In the sorted form.
> >
>
> I have no idea what any of this means. Post real examples, real code,
> real input and real output.
>
> > > Show us with *real* data. Provide
> > > example sample input, and the corresponding output you want to achieve
> > > from it.
> >
> >
> > 1000033 (internal code)
> > title
> > discription
> > pictureURL
> > 165 (ranking number)
> > 30 (item number)
> >
> > And I want to sort the file by the "ranking number", largest number
> > on top of the record file, keeping all the other information together
> > with this record, in the sort.
>
> You still haven't shown *real* data. You've shown exactly one record.
> What delimits records in the file? How do you know where one record
> ends and another begins? Your either making assumptions, or asking us
> to.
>
> > The file name this information comes
> > from is "temp.txt". The above information goes to "temp.txt" then is
> > to be sorted, then the "temp.txt" file is to be renamed to the original
> > file
> > name to replace it with the sorted records. Which makes sure the
> > highest ranking number is always on top of the web page.
>
> I have difficulty parsing any of this. Again, *real* data, speaking
> Perl rather than English, would be much more clear and helpful.
>
> > > #!/usr/bin/perl
> > > use strict;
> > > use warnings;
> > >
> > > #Using the Schwartzian transfer, create the
> > > #anonymous array ref to hold the fourth field
> > > #of each line, then compare on those fields
> > > my @sorted = map { $_->[0] }
> > > sort { $a->[1] <=> $b->[1] }
> > > map { [$_, (split /\|/)[3]] } ;
> > > print "Sorted:\n", @sorted, "\n";
> > >
> >
> >
> > I will give this a try, but I don't know if split is needed, because
> > the record is as above:
> >
> > A
> > B
> > C
> > D
> >
> > not A B C D on one line in the file, as below.
>
> Okay. I will make an assumption, then, that your "records" are simply
> sequences of 6 lines, where the fifth line contains the data on which
> you want to base your sort.
>
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> my @records;
> my $curline;
> #read each line
> while (){
> $curline .= $_;
> #every sixth line, store a record
> if ($. % 6 == 0) {
> push @records, $curline;
> undef $curline;
> }
> }
>
> #now we have an array of the records.
> my @sorted = map { $_->[0] }
> sort { $b->[1] <=> $a->[1] }
> map { [ $_, (split /\n/)[4] ] } @records;
>
> print @sorted;
>
> __DATA__
> 1000033
> title
> discription
> pictureURL
> 165
> 30
> 1000034
> title
> discription
> pictureURL
> 160
> 30
> 1000035
> title
> discription
> pictureURL
> 170
> 30
>
>
> Output:
> 1000035
> title
> discription
> pictureURL
> 170
> 30
> 1000033
> title
> discription
> pictureURL
> 165
> 30
> 1000034
> title
> discription
> pictureURL
> 160
> 30
>
> Reading from the file, printing to a new file, and renaming the files
> are all left as exercises for you to work on. Read the documentation
> (see below), and then ask if you have problems getting it to work:
> perldoc -f open
> perldoc perlopentut
> perldoc -f print
> perldoc -f rename
>
> Paul Lalli
Re: Sorting Records
am 23.11.2005 07:55:09 von Gunnar Hjalmarsson
flyaway840@hotmail.com wrote:
> I don't know what it is about perl groups or physics groups, but
> if you do ask a question usually you get an answer as, what are
> you some kind of retard. With expecting you to have all the
> information prior to asking the question. As: "Then you have a
> reading comprehension problem. perldoc -f sort includes
> example code that does just that." So we were supposed to
> have known -f does soemthing we did not know in the first
> place.
Excuse me, but that was in response to you saying "I have read all the
sort etc. in perldoc and they did not help". Did you lie?
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Re: Sorting Records
am 23.11.2005 13:57:33 von Paul Lalli
flyaway840@hotmail.com wrote:
> I don't know what it is about perl groups or physics groups, but
> if you do ask a question usually you get an answer as, what are
> you some kind of retard.
No, you get poor answers if you ask poor questions. Ask better
questions, you get better answers. Simple, really.
> With expecting you to have all the information prior to asking the question.
Absolutely not. You are expected to *try to find* the answer on your
own before asking the question. Significantly different.
> As: "Then you have a
> reading comprehension problem. perldoc -f sort includes
> example code that does just that."
You claimed that you read all the documentation for sort!
> So we were supposed to
> have known -f does soemthing we did not know in the first
> place.
First: what, exactly, did you read that enabled you to claim you read
all the perldoc for sort, if you didn't read "perldoc -f sort"?
Second: what the heck are you doing trying to program in Perl without
first reading the documentation that comes with Perl? Do you always
discard the user's manual of every new product before trying to use it?
Read, in this order:
perldoc perldoc
perldoc perl
perldoc perlintro
> If it pisses those off for us to ask questions, or to ask to
> find the document to read, how are we supposed to ask those
> for it?
You did *not* ask how to find a document to read. You asked someone to
give you the answer outright, after claiming that you had read the
documentation, which is becoming more and more obviously a lie.
> When someone says please don't just say see
> perldoc whatever that means they have tried to do it on their
> own and need help.
No, about 85% of the time, I'd wager, it means they don't want to read
any documentation and just want the answer given to them.
> Calling us retards is not help, it is arrogance.
On the contrary. Pointing someone to documentation (no one - anywhere
in this thread - called you a "retard") assumes that you are at least
as intelligent as I am. It means I assume you have the mental capacity
to learn from the documentation as well as I learned from the
documentation. It is a compliment, not an insult.
> Thanks for the code below, I will give it a try. And your assumption
> was correct, data is on a single line, only the count of 6 X 6 ...
> separates them. You know one record from the other by the
> count. If it is line 7 it is the next record.
So why didn't you specifiy that originally? You ask poor questions,
you get answers that are either poor, sub-optimal, full of assumptions,
or flat out wrong.
> As in sequential cobal files.
Here you're making an assumption that I, or anyone else in the Perl
newsgroups, knows what Cobal files look like.
Paul Lalli
Re: Sorting Records
am 24.11.2005 04:41:13 von flyaway840
Paul Lalli wrote:
> flyaway840@hotmail.com wrote:
> > I don't know what it is about perl groups or physics groups, but
> > if you do ask a question usually you get an answer as, what are
> > you some kind of retard.
>
> No, you get poor answers if you ask poor questions. Ask better
> questions, you get better answers. Simple, really.
>
> > With expecting you to have all the information prior to asking the question.
>
> Absolutely not. You are expected to *try to find* the answer on your
> own before asking the question. Significantly different.
Ok the next time you are in a new big city *try to find* 4th and
Vine before you ask where it is, does that make sense? And
Einstein said you have to know the answer before you can ask
the right questions, unless you know the answer you have to
stumble through to find your way. If you are a beginner you don't
know what to ask.
And besides this is now a mute point, because if one is a good
programmer (which I am, just not in perl). you think of the most
efficient way to do the task. A sort takes up a lot of processing
time and effort, and what I really need is only to stick the largest
value above the one below it. I can do that with 3 arrays, one
the file, two the record below the first, three to hold the first
record
to put it into the second record spot. Problem solved. The program
is like a top banner program where the rating value is only incremented
by 1 , not like 45 , so a sort is not needed.
>
> > As: "Then you have a
> > reading comprehension problem. perldoc -f sort includes
> > example code that does just that."
>
> You claimed that you read all the documentation for sort!
And I am a beginner and did not know that there is more.
Again you seem unable to understand the concept of a
beginner.
>
> > So we were supposed to
> > have known -f does soemthing we did not know in the first
> > place.
>
> First: what, exactly, did you read that enabled you to claim you read
> all the perldoc for sort, if you didn't read "perldoc -f sort"?
>
> Second: what the heck are you doing trying to program in Perl without
> first reading the documentation that comes with Perl?
And you are assuming we have access to Perl documentation. I
started out loading things to my cgi-bin and trying them out on
my web site. I did not even know you can find programs to
try your perl programs out on your computer.
Again you seem unable to understand the concept of a
beginner.
> Do you always
> discard the user's manual of every new product before trying to use it?
Yes. Until I run into something I cannot figure out. Would a valid
question then be, are you so lacking in intelligence you cannot
figure anything out without a manual? How did you manage on
your first date. I know how to program, I don't know the perl
language. I don't need to relearn logic, I just need to learn how
perl does what I have been doing for years in other languages.
> Read, in this order:
> perldoc perldoc
> perldoc perl
> perldoc perlintro
Thanks
>
> > If it pisses those off for us to ask questions, or to ask to
> > find the document to read, how are we supposed to ask those
> > for it?
>
> You did *not* ask how to find a document to read. You asked someone to
> give you the answer outright, after claiming that you had read the
> documentation, which is becoming more and more obviously a lie.
Again you seem unable to understand the concept of a
beginner.
>
> > When someone says please don't just say see
> > perldoc whatever that means they have tried to do it on their
> > own and need help.
>
> No, about 85% of the time, I'd wager, it means they don't want to read
> any documentation and just want the answer given to them.
So, if you don't want to do that don't answer, simple as that. Your
lot
seem to think teaching is a bad thing to do, you unlike the others
did tried very hard to show the code, (it did not work, but you tried).
To show off my superiority I show I know more than others, (like
you did begrudgingly) not call them a retard, and say look it up for
yourself. If I know the answer I am hapy to give it, not call them lazy
because they can't figure it out for themselves.
>
> > Calling us retards is not help, it is arrogance.
>
> On the contrary. Pointing someone to documentation (no one - anywhere
> in this thread - called you a "retard") assumes that you are at least
> as intelligent as I am. It means I assume you have the mental capacity
> to learn from the documentation as well as I learned from the
> documentation. It is a compliment, not an insult.
>
> > Thanks for the code below, I will give it a try. And your assumption
> > was correct, data is on a single line, only the count of 6 X 6 ...
> > separates them. You know one record from the other by the
> > count. If it is line 7 it is the next record.
>
> So why didn't you specifiy that originally?
I did, when you see:
You have a record with
A
B
C
D
E
F
that should tell you it is a sequential file with the data on
each line. I did not put A,B,C,D,E,F, or A:B:C:D:E:F
> You ask poor questions,
> you get answers that are either poor, sub-optimal, full of assumptions,
> or flat out wrong.
>
> > As in sequential cobal files.
>
> Here you're making an assumption that I, or anyone else in the Perl
> newsgroups, knows what Cobal files look like.
And you're making an assumption that I, or anyone else as a beginner
knows we should know how to look anything up in perldoc, that we
have access on our own computer to perldoc, and have perl and
apache installed on our computers.
Seems hypocritical to me.
I was not trying to piss you off, I appreciate your help and this post
is not to combat you, but hopefully to teach you that beginners are
totally ignorant, and you have to assume that, not that all they have
to do is look something up in the magical perldoc and (like turning
18) they magaically know everything there is to know on the subject.
Thanks again
>
> Paul Lalli
Re: Sorting Records
am 24.11.2005 10:18:13 von Joe Smith
flyaway840@hotmail.com wrote:
> And you are assuming we have access to Perl documentation.
We assume that everyone posting to this newsgroup knows how to do
a Google search on "perl documentation".
Google knows about http://perldoc.perl.org
Re: Sorting Records
am 25.11.2005 14:12:04 von Paul Lalli
flyaway840@hotmail.com wrote:
> Paul Lalli wrote:
> > flyaway840@hotmail.com wrote:
> > > With expecting you to have all the information prior to asking the question.
> >
> > Absolutely not. You are expected to *try to find* the answer on your
> > own before asking the question. Significantly different.
>
> Ok the next time you are in a new big city *try to find* 4th and
> Vine before you ask where it is, does that make sense?
Good analogy, actually. And you know what I'd do? I'd *look at a map*
before asking random people for the answer. Why is that a difficult
concept?
> If you are a beginner you don't know what to ask.
I wholly disagree. Every beginner should know exactly what to ask:
"Where and how can I learn about this topic?"
> And besides this is now a mute point, because if one is a good
> programmer (which I am, just not in perl). you think of the most
> efficient way to do the task. A sort takes up a lot of processing
> time and effort, and what I really need is only to stick the largest
> value above the one below it. I can do that with 3 arrays, one
> the file, two the record below the first, three to hold the first
> record
> to put it into the second record spot. Problem solved. The program
> is like a top banner program where the rating value is only incremented
> by 1 , not like 45 , so a sort is not needed.
Ah, so now you're telling us that what you originally asked for is not
what you actually wanted. Yet another example of your poor questioning
style.
> > > As: "Then you have a
> > > reading comprehension problem. perldoc -f sort includes
> > > example code that does just that."
> >
> > You claimed that you read all the documentation for sort!
>
> And I am a beginner and did not know that there is more.
Then how do you feel you have the right to claim that you *read it
all*!? Simply don't make claims that you cannot possibly know to be
true, and none of these issues would have arrisen in the first place!
> > > So we were supposed to
> > > have known -f does soemthing we did not know in the first
> > > place.
> >
> > First: what, exactly, did you read that enabled you to claim you read
> > all the perldoc for sort, if you didn't read "perldoc -f sort"?
> >
> > Second: what the heck are you doing trying to program in Perl without
> > first reading the documentation that comes with Perl?
>
> And you are assuming we have access to Perl documentation.
Yes, I am. Everyone who has Perl installed has access to the Perl
documentation.
> Again you seem unable to understand the concept of a beginner.
Your issues had nothing to do with your being a beginner. They have to
do with you making assumptions, false claims, and a lack of thorough
research.
> > Do you always
> > discard the user's manual of every new product before trying to use it?
>
> Yes. Until I run into something I cannot figure out.
Except that's not what you did. You ran into something you couldn't
figure out, and instead of looking for the manual - or even asking if a
manual exists - you claimed that you'd read all there was to read
(without having anyway of knowing what there is to read), and asked
people to give you the answer.
> Would a valid
> question then be, are you so lacking in intelligence you cannot
> figure anything out without a manual? How did you manage on
> your first date.
Ahh, and now we delve into personal insults. Good show.
> I know how to program, I don't know the perl
> language. I don't need to relearn logic, I just need to learn how
> perl does what I have been doing for years in other languages.
Again, then why isn't THIS what you asked?
read:
perldoc perltrap
> > > If it pisses those off for us to ask questions, or to ask to
> > > find the document to read, how are we supposed to ask those
> > > for it?
> >
> > You did *not* ask how to find a document to read. You asked someone to
> > give you the answer outright, after claiming that you had read the
> > documentation, which is becoming more and more obviously a lie.
>
> Again you seem unable to understand the concept of a beginner.
You seem unable to understand the concept of *beginning*, rather than
having everything handed to you.
> > > When someone says please don't just say see
> > > perldoc whatever that means they have tried to do it on their
> > > own and need help.
> >
> > No, about 85% of the time, I'd wager, it means they don't want to read
> > any documentation and just want the answer given to them.
>
> So, if you don't want to do that don't answer, simple as that. Your
> lot seem to think teaching is a bad thing to do,
I must admit I find this exceedingly amusing, as I am a professional
Perl instructor at a college.
> you unlike the others
> did tried very hard to show the code, (it did not work, but you tried).
> To show off my superiority I show I know more than others,
I'm sorry, what? You know more than others? Not a thing in this
thread supports that claim.
> > > You know one record from the other by the
> > > count. If it is line 7 it is the next record.
> >
> > So why didn't you specifiy that originally?
>
> I did, when you see:
>
> You have a record with
>
> A
> B
> C
> D
> E
> F
>
> that should tell you it is a sequential file with the data on
> each line. I did not put A,B,C,D,E,F, or A:B:C:D:E:F
My point was that you did not specify how one record ended and another
began. You showed only one record, and left it for us to assume that
it would be "obvious" how your data is formatted.
> And you're making an assumption that I, or anyone else as a beginner
> knows we should know how to look anything up in perldoc,
No, I assume that if you're not asking where the documentation is, that
you've already found it.
> that we
> have access on our own computer to perldoc,
Which you do, or at the very least, can easily obtain through minimal
effort.
> and have perl and apache installed on our computers.
When the hell did apache come into this conversation? What does apache
have to do with anything?
> I was not trying to piss you off, I appreciate your help and this post
> is not to combat you, but hopefully to teach you that beginners are
> totally ignorant,
And I'm trying to teach you that ignorance is not a sin. Asking poor
questions, making false claims, and not doing proper research is.
> and you have to assume that, not that all they have
> to do is look something up in the magical perldoc and (like turning
> 18) they magaically know everything there is to know on the subject.
Wow, you knew everything there is to know about "the subject" (whatever
that is) just by turning 18? Impressive...
Paul Lalli
Re: Sorting Records
am 08.12.2005 04:12:12 von harryooopotter
Paul Lalli wrote...
>Okay. I will make an assumption, then, that your "records" are simply
>sequences of 6 lines, where the fifth line contains the data on which
>you want to base your sort.
>
>#!/usr/bin/perl
>use strict;
>use warnings;
>
>my @records;
>my $curline;
>#read each line
>while (){
> $curline .= $_;
> #every sixth line, store a record
> if ($. % 6 == 0) {
> push @records, $curline;
> undef $curline;
> }
>}
>
>#now we have an array of the records.
>my @sorted = map { $_->[0] }
> sort { $b->[1] <=> $a->[1] }
> map { [ $_, (split /\n/)[4] ] } @records;
>
>print @sorted;
>
>__DATA__
>1000033
>title
>discription
>pictureURL
>165
>30
Thank Paul very much for this great example.
I would like to ask for the possible solution with two changes
in the data input.
- the input data is usually a large text file, or comming
from STDIN;
- the multi-line records are of variable length;
but a blank line is the record separator.
e.g. (assume the 5th line is the sort-key; and every record
has >= 5 lines)
-- input.txt -- begin --
1000033
title
discription
pictureURL
165
30
1000037
title
discription
pictureURL
160
more lines
more lines
1000020
title
discription
pictureURL
173
more lines
more lines
more lines
-- input.txt -- end --
How to sort this input.txt with the 5th line as the sort key?
TIA
Re: Sorting Records
am 08.12.2005 06:51:21 von harryooopotter
Harry wrote...
>Paul Lalli wrote...
>>#!/usr/bin/perl
>>use strict;
>>use warnings;
>>
>>my @records;
>>my $curline;
>>#read each line
>>while (){
>> $curline .= $_;
>> #every sixth line, store a record
>> if ($. % 6 == 0) {
>> push @records, $curline;
>> undef $curline;
>> }
>>}
>>
>>#now we have an array of the records.
>>my @sorted = map { $_->[0] }
>> sort { $b->[1] <=> $a->[1] }
>> map { [ $_, (split /\n/)[4] ] } @records;
>>
>>print @sorted;
>>
>>__DATA__
>>1000033
>>title
>>discription
>>pictureURL
>>165
>>30
>
>Thank Paul very much for this great example.
>
>I would like to ask for the possible solution with two changes
>in the data input.
> - the input data is usually a large text file, or comming
> from STDIN;
> - the multi-line records are of variable length;
> but a blank line is the record separator.
>
>e.g. (assume the 5th line is the sort-key; and every record
> has >= 5 lines)
Never mind; I got it.
#!/usr/bin/perl
use strict;
use warnings;
my @records;
my $curline;
#read each line
while (<>){
$curline .= $_;
# if blank-line, store a record
if (/^\s*$/) {
push @records, $curline;
undef $curline;
}
}
#now we have an array of the records.
my @sorted = map { $_->[0] }
sort { $a->[1] <=> $b->[1] }
map { [ $_, (split /\n/)[4] ] } @records;
print @sorted;
$ ./sort.pl < input.txt
Re: Sorting Records
am 08.12.2005 11:19:26 von Joe Smith
Harry wrote:
> - the input data is usually a large text file, or comming
> from STDIN;
> - the multi-line records are of variable length;
> but a blank line is the record separator.
Take a look at 'perldoc perlvar'. Look for the section where it
talks about INPUT_RECORD_SEPARATOR. Simply setting $/ to the null
string causes perl to read an entire record at a time, where records
are separated by blank lines.
$/ = "";
@records = <>;
or
$/ = ""
while(<>){ ... }
-Joe
Re: Sorting Records
am 08.12.2005 20:53:06 von harryooopotter
Joe Smith wrote...
>
>Harry wrote:
>
>> - the input data is usually a large text file, or comming
>> from STDIN;
>> - the multi-line records are of variable length;
>> but a blank line is the record separator.
>
>Take a look at 'perldoc perlvar'. Look for the section where it
>talks about INPUT_RECORD_SEPARATOR. Simply setting $/ to the null
>string causes perl to read an entire record at a time, where records
>are separated by blank lines.
>
> $/ = "";
> @records = <>;
>
>or
>
> $/ = ""
> while(<>){ ... }
Thanks Joe for the info.
However, the following script did not sort the input.txt. That was,
input and output were identical.
What did I do wrong?
$ cat sort2.pl
#!/usr/bin/perl
use strict;
use warnings;
my @records;
my $sort_key = 5; # 5-th line of a record is the sort-key
$/ = ""; # record separator is a blank line
@records = <>; # read all records from STDIN
#now we have an array of the records.
my @sorted = map { $_->[0] }
sort { $a->[1] <=> $b->[1] }
map { [ $_, (split /\n/)[$sort_key - 1] ] } @records;
print @sorted;
$ ./sort2.pl < input.txt
1000033
title
discription
pictureURL
165
line1 after the sort-key
1000034
title
discription
pictureURL
160
line1 after the sort-key
line2 after the sort-key
1000035
title
discription
pictureURL
170
line1 after the sort-key
line2 after the sort-key
line3 after the sort-key
$
Re: Sorting Records
am 08.12.2005 22:09:33 von harryooopotter
Harry wrote...
>
>Joe Smith wrote...
>> Simply setting $/ to the null
>>string causes perl to read an entire record at a time, where records
>>are separated by blank lines.
>>
>> $/ = "";
>> @records = <>;
>Thanks Joe for the info.
>
>However, the following script did not sort the input.txt. That was,
>input and output were identical.
>What did I do wrong?
>
>$ cat sort2.pl
>#!/usr/bin/perl
>use strict;
>use warnings;
>
>my @records;
>my $sort_key = 5; # 5-th line of a record is the sort-key
>
>$/ = ""; # record separator is a blank line
OK, I got it.
On my cygwin environment: the pattern "\r\n\r\n" corresponds to
a blank line.
So,
$/ = "\r\n\r\n"
will fix my problem.
>@records = <>; # read all records from STDIN
>
>#now we have an array of the records.
>my @sorted = map { $_->[0] }
> sort { $a->[1] <=> $b->[1] }
> map { [ $_, (split /\n/)[$sort_key - 1] ] } @records;
>
>print @sorted;
>
>$ ./sort2.pl < input.txt