Simple way to Reverse an Array

P

Peter Sundstrom

Uri Guttman said:
PS> I'm usually pretty quick actually. Keep in mind that my timezone
PS> is GMT-12 and the time the OP's question reached me was 5:20am and
PS> I updated the website at 8:55am (localtime), I'd say that's pretty
PS> good :)

sorry for the slight insult! :) and i see the updated page now. just
curious, but could you put in the date of each entry? then we could
track when SAQ's get created.

I'll do that from now on.

and IIRC when i last emailed you an SAQ i noticed your page hasn't been
updated in a while. maybe i was thinking that and it wasn't your
response time. that just means we don't get enough SAQ's here (or
elsewhere)!

Perl SAQ's tend to come in a group and then go really quiet for a long time.
Expect a couple more in the next few weeks :)
 
U

usenet

Peter said:
Please explain? Do you mean non-escaping of characters such as < and >? I
was under the impression that if they are in a <pre> tag block you don't
need to escape them.

FWIW, the page displays properly on my systems (Firefox on SuSE and
Windows). I dusted off IE on XP and it looks OK there also.
The Perl SAQ page is maintained manually with vi, the editor of champions
Hee, hee - even Bill Joy has said he cannot understand why anyone still
uses vi :^}

And he wrote it.
 
U

usenet

Bill said:
Actually I am trying to read it backwards so that I can display the
most recent entry first (it is a simple log file).

Are you saying you don't understand that you can read it forwards into
an array and just use the reverse() function to process (ie, display)
it backwards?

Of course, if you REALLY want to read the file backwards, you can use
the File::ReadBackwards module (I'm not making this up - see
http://search.cpan.org/~uri/File-ReadBackwards-1.04/ReadBackwards.pm).

Or, the IO::All module acts as a proxy for File::ReadBackwards, and
IO::All makes your life easy, so it would be as simple as:

#!/usr/bin/perl
use strict; use warnings;
use IO::All;

print io('file.txt') -> backwards -> slurp; # EASY!

__END__

Of course, filehandles, opening & closing, and error handling/reporting
are all provided by the IO::All module.
 
U

usenet

A. Sinan Unur said:
Abuse report sent.

I couldn't find an abuse-report contact address on MCNC's website. Did
you just assume that abuse@... would work (as it often does), or did
you find actual abuse contact information?
 
A

A. Sinan Unur

(e-mail address removed) wrote in @g43g2000cwa.googlegroups.com:
I couldn't find an abuse-report contact address on MCNC's website. Did
you just assume that abuse@... would work (as it often does), or did
you find actual abuse contact information?

Injection-Info: g47g2000cwa.googlegroups.com; posting-host=152.31.32.65;

C:\> whois 152.31.32.65

OrgName: North Carolina Research and Education Network

I sent to report both to GG and ncret.net. Let each sort out what they can
do.

Sinan
 
A

A. Sinan Unur

(e-mail address removed) wrote in @g44g2000cwa.googlegroups.com:
Are you saying you don't understand that you can read it forwards into
an array and just use the reverse() function to process (ie, display)
it backwards?

Of course, if you REALLY want to read the file backwards, you can use
the File::ReadBackwards module (I'm not making this up - see
http://search.cpan.org/~uri/File-ReadBackwards-1.04/ReadBackwards.pm).

If the log files are large, reading it in small chunks starting from end,
and proceeding towards the top will be more practical.

Sinan
 
U

Uri Guttman

ASU> (e-mail address removed) wrote in ASU> @g44g2000cwa.googlegroups.com:

ASU> If the log files are large, reading it in small chunks starting
ASU> from end, and proceeding towards the top will be more practical.

and how do you think File::ReadBackwards works? by reading the file
forward and calling reverse on that list? please check out the module's
docs before you say what would be more practical. :)

uri
 
M

Matt Garrish

Uri Guttman said:
ASU> (e-mail address removed) wrote in ASU> @g44g2000cwa.googlegroups.com:


ASU> If the log files are large, reading it in small chunks starting
ASU> from end, and proceeding towards the top will be more practical.

and how do you think File::ReadBackwards works? by reading the file
forward and calling reverse on that list? please check out the module's
docs before you say what would be more practical. :)

Just cause you slapped your name on it doesn't convince me you know how it
works... : )

Matt
 
A

A. Sinan Unur

ASU> (e-mail address removed) wrote in ASU> @g44g2000cwa.googlegroups.com:


ASU> If the log files are large, reading it in small chunks starting
ASU> from end, and proceeding towards the top will be more
practical.

and how do you think File::ReadBackwards works? by reading the file
forward and calling reverse on that list? please check out the
module's docs before you say what would be more practical. :)

I think you missed part of my point, and jumped the gun a little here. I
know what File::ReadBackwards does ... Inded, I am the one who first
suggested it in this thread. See:

http://groups.google.com/group/comp.lang.perl.misc/msg/a811a14fbe6e7353

The capitalization in David Filmer's 'REALLY' implied to me that he was
not convinced that it was a good idea to read the log file backwards.
So, I tried to explain to him and others why using your module is a
better idea than

my @log = reverse <$log>;

Hope this clarifies things a little.

Sinan
 
U

Uri Guttman

ASU> (e-mail address removed) wrote in ASU> @g44g2000cwa.googlegroups.com:ASU> If the log files are large, reading it in small chunks starting
ASU> from end, and proceeding towards the top will be more practical.
MG> Just cause you slapped your name on it doesn't convince me you
MG> know how it works... : )

well, you figured me out! i copied the guts of it from some script i
found on matt's archive. are you that matt? your code is so e133t i had
to steal it! i learned so much about efficiency and quality perl coding
from it.

:)

uri
 
U

Uri Guttman

ASU> (e-mail address removed) wrote in ASU> @g44g2000cwa.googlegroups.com:ASU> If the log files are large, reading it in small chunks starting
ASU> from end, and proceeding towards the top will be more
ASU> I think you missed part of my point, and jumped the gun a little here. I
ASU> know what File::ReadBackwards does ... Inded, I am the one who first
ASU> suggested it in this thread. See:

ASU> http://groups.google.com/group/comp.lang.perl.misc/msg/a811a14fbe6e7353

ASU> The capitalization in David Filmer's 'REALLY' implied to me that he was
ASU> not convinced that it was a good idea to read the log file backwards.
ASU> So, I tried to explain to him and others why using your module is a
ASU> better idea than

ASU> Hope this clarifies things a little.

makes a little more sense now. but then i tend to read posts backwards
which is why i wrote that module. must be all the top posting i see all
over! :)

uri
 
A

A. Sinan Unur

....

ASU> Hope this clarifies things a little.

makes a little more sense now.

By the way, a belated thanks for the module. It saved me time and effort
quite a few times.
but then i tend to read posts backwards which is why i wrote that
module. must be all the top posting i see all over! :)

;-)
 
U

Uri Guttman

ASU> ...

ASU> Hope this clarifies things a little.
ASU> By the way, a belated thanks for the module. It saved me time and
ASU> effort quite a few times.

and the ironic thing is that i have never actually used this module
myself. i just don't ever seem to need to process logs and other large
files backwards. i wrote it to quiet the constant request for an easy
and fast way to do this. the perl cookbook 1st ed. has an awful solution
of scanning the file forward by lines and storing each line's seek point
in an array and then using that to read backwards by seeking to and
reading a line.

it was also a good exercise in writing a module that was focused on
those two requirements. i think it makes for an interesting piece of
well written code that would be worth reading by anyone into perl. of
course your comments on it are welcome and will be redirected to
/dev/null. :) and it seems to have become the community standard way to
do do this.

uri
 
M

Matt Garrish

Uri Guttman said:
ASU> (e-mail address removed) wrote in ASU> @g44g2000cwa.googlegroups.com:
ASU> If the log files are large, reading it in small chunks starting
ASU> from end, and proceeding towards the top will be more practical.

MG> Just cause you slapped your name on it doesn't convince me you
MG> know how it works... : )

well, you figured me out! i copied the guts of it from some script i
found on matt's archive. are you that matt? your code is so e133t i had
to steal it! i learned so much about efficiency and quality perl coding
from it.

Props to me and my archive, d00d! ; )

Matt
 
D

Dr.Ruud

Dr.Ruud:
A. Sinan Unur:

echo '(e-mail address removed)' \
| perl -ple '@_=/(\w+|.)/g;pop@_,pop@_;$_=reverse@_=reverse@_'

| perl -ple 'split/(\W)/;pop@_;pop@_;$_=reverse@_=reverse@_'
 
A

Anno Siegel

Dr.Ruud said:
Dr.Ruud:

| perl -ple 'split/(\W)/;pop@_;pop@_;$_=reverse@_=reverse@_'

| perl -ple 's/\.[^.]+$//;s/(\w+)/reverse$1/eg'

Much more pedestrian, but shorter is shorter.

Anno, who used to say he doesn't do golf.
 
G

googoomagoo

A. Sinan Unur said:
(e-mail address removed) wrote in


That's pure genius. Were you able to come up with that all on
your own?

I would like to bring to your attention:

http://www.mcnc.org/index.cfm?fuseaction=page&filename=ncren_acceptable_use.html

c. Neither Subscriber nor Subscriber's users shall stalk others, post,
transmit, or originate any unlawful, threatening, abusive, fraudulent,
hateful, defamatory, obscene, or pornographic communication,

Abuse report sent.

Sinan

HAHAHA A.Sinine Usurer got mad.

Check out what students say about him at ratemyprofessors.com:

Gets confused quite easily with information. Review for Final was a 3
hour session which he emphasized a whole area to memorize only to not
include it in the final at all.

Most retarded teacher at Cornell; he couldn't even solve 2x=12 to
derive the eq. pt.

Unur is useless at life. You do not learn a single thing in this class.
It's a joke.

(e-mail address removed)
(e-mail address removed)
(e-mail address removed)
 
D

Dr.Ruud

Anno Siegel:
Dr.Ruud:
Dr.Ruud:

| perl -ple 'split/(\W)/;pop@_;pop@_;$_=reverse@_=reverse@_'

| perl -ple 's/\.[^.]+$//;s/(\w+)/reverse$1/eg'

Much more pedestrian, but shorter is shorter.

Cool, but out of topic. <g>

It can be shortened to(o):

| perl -ple 's/.\w+$//;s/(\w+)/reverse$1/eg'
 
A

A. Sinan Unur

(e-mail address removed) wrote in

[ I apologize for falling for this troll but I am posting this just in
case any of my students stumbles upon this posting. I will, under no
circumstances, post on this thread again. ]
A. Sinan Unur wrote: ....

Check out what students say about him at ratemyprofessors.com:

The URL is:

http://www.ratemyprofessors.com/ShowRatings.jsp?tid=479462

Honestly, over the last 15 years during which I have been teaching in
one capacity or another, I have received both better and worse
evaluations.

I try to give my students the service they deserve for the money they
are spending on their education.

Every time I receive feedback pointing out my shortcomings, I do my best
to improve myself. It is even better if the critcism is accompanied with
suggestions on how things can be improved.

Sinan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top