Simple way to Reverse an Array

B

Bill H

If I use the following to load a file into an array, is there a simple
way of reversing the arrays contents?

open(FILE,"sometextfile.txt");
@THETEXTFILE = <FILE>;
close(FILE);

So $THETEXTFILE[0] holds line 1, [1] holds line 2 ect. other than doing
something like this is there a way to reverse the values?

for($i = @THETEXTFILE -1;$i >=0;$i++)
{
$THENEWFILE[@THENEWFILE] = $THETEXTFILE[$i];
}

I am just typing this in, not pasting actual code.

Bill H www.ts1000.us
 
J

Jürgen Exner

Bill said:
If I use the following to load a file into an array, is there a simple
way of reversing the arrays contents?

Has been a long time but finally we got a new SAQ (Self Answering Question).

Did you check "perldoc -f reverse"?

jue
 
P

Paul Lalli

Bill said:
If I use the following to load a file into an array, is there a simple
way of reversing the arrays contents?

open(FILE,"sometextfile.txt");
@THETEXTFILE = <FILE>;
close(FILE);

So $THETEXTFILE[0] holds line 1, [1] holds line 2 ect. other than doing
something like this is there a way to reverse the values?

for($i = @THETEXTFILE -1;$i >=0;$i++)
{
$THENEWFILE[@THENEWFILE] = $THETEXTFILE[$i];
}

I am just typing this in, not pasting actual code.

You have asked a SAQ: Self-Answering Question

Please do a *minimal* amount of searching before asking thousands of
people to read the documentation to you.

perldoc -f reverse
http://perldoc.perl.org/functions/reverse.html

Paul Lalli
 
U

Uri Guttman

BH> If I use the following to load a file into an array, is there a simple
BH> way of reversing the arrays contents?

SAQ!!

it deserves to be on this page (author is cc'ed)

http://www.ginini.com/perlsaq.html

try looking up the very word you used to describe the function you want
to perform. search perldoc perlfunc for it.

uri
 
B

Bill H

Jürgen Exner said:
Has been a long time but finally we got a new SAQ (Self Answering Question).

Did you check "perldoc -f reverse"?

jue

Thanks for pointing this out to me, knew there was an easy way to do it.
 
A

A. Sinan Unur

If I use the following to load a file into an array, is there a simple
way of reversing the arrays contents?

open(FILE,"sometextfile.txt");
@THETEXTFILE = <FILE>;
close(FILE);

So $THETEXTFILE[0] holds line 1, [1] holds line 2 ect. other than
doing something like this is there a way to reverse the values?

Your basic question has been answered, but is there any chance you are
actually trying to read the file backwards?

See http://search.cpan.org/~uri/File-ReadBackwards-1.04/ReadBackwards.pm

Sinan
 
U

Uri Guttman

u> So has anyone actually let Peter know?

i cc'ed him on my previous post in this thread.

he tends to be slow to respond or update that page. i had to google to
find it and it wasn't even found by google (at least on the front page!)
but several others had links to it. it is always an amusing read!

uri
 
B

Bill H

A. Sinan Unur said:
If I use the following to load a file into an array, is there a simple
way of reversing the arrays contents?

open(FILE,"sometextfile.txt");
@THETEXTFILE = <FILE>;
close(FILE);

So $THETEXTFILE[0] holds line 1, [1] holds line 2 ect. other than
doing something like this is there a way to reverse the values?

Your basic question has been answered, but is there any chance you are
actually trying to read the file backwards?

See http://search.cpan.org/~uri/File-ReadBackwards-1.04/ReadBackwards.pm

Actually I am trying to read it backwards so that I can display the
most recent entry first (it is a simple log file). I will look at this
link you sent in more detail.

Bill H
 
F

foofoomagoo

Uri said:
u> So has anyone actually let Peter know?

i cc'ed him on my previous post in this thread.

he tends to be slow to respond or update that page. i had to google to
find it and it wasn't even found by google (at least on the front page!)
but several others had links to it. it is always an amusing read!

So are your posts, Urine Buttman. Learn how to use the Shift key,
fucktard!
 
P

Peter Sundstrom

Uri Guttman said:
u> So has anyone actually let Peter know?

i cc'ed him on my previous post in this thread.

he tends to be slow to respond or update that page. i had to google to
find it and it wasn't even found by google (at least on the front page!)
but several others had links to it. it is always an amusing read!

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

A. Sinan Unur

(e-mail address removed) wrote in
So are your posts, Urine Buttman. Learn how to use the Shift key,
fucktard!

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
 
P

Peter Sundstrom

Paul Lalli said:
That page is suffering from a lack of CGI::escapeHTML() (or the
equivalent for whatever you're using to maintain the site). :)

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. Of course, this is getting a bit of topic for clpm.

The Perl SAQ page is maintained manually with vi, the editor of champions
;-)
 
P

Paul Lalli

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. Of course, this is getting a bit of topic for clpm.

Yes, that's what I was talking about. And I can find no mention of
<pre> removing the requirement for escaping entities at
http://www.w3.org/TR/REC-html40/struct/text.html#h-9.3.4

Indeed, the page viewed both in IE and Firefox (the only browsers I
have available right now) both show the < > as rendering (non-existant)
tags, rather than the characters themselves...

Paul Lalli
 
U

Uri Guttman

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.

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)!

and at least i didn't pull out my 3rd grade insult dictionary and abuse
you with that! :)


uri
 
U

Uri Guttman

PL> Indeed, the page viewed both in IE and Firefox (the only browsers I
PL> have available right now) both show the < > as rendering (non-existant)
PL> tags, rather than the characters themselves...

i see <> just fine in the quoted areas.

$hash->{'fred"}
ls > /tmp/workfile
@arr = `cat /etc/passwd 2>&1 `;
123<#>38923<#>hewfwiehi

this is firefox 1.5 on linux.

i can try exploder but i don't have enough pain killers at the moment to
do so.

uri
 
P

Peter Sundstrom

Paul Lalli said:
clpm.

Yes, that's what I was talking about. And I can find no mention of
<pre> removing the requirement for escaping entities at
http://www.w3.org/TR/REC-html40/struct/text.html#h-9.3.4

Indeed, the page viewed both in IE and Firefox (the only browsers I
have available right now) both show the < > as rendering (non-existant)
tags, rather than the characters themselves...

You are quite correct. It just so happens that all the previous entries
(apart from the two newest) didn't have any filehandles in the code, hence
the reason why I never picked it up until now.

I've now corrected the escaping.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top