Simple way to Reverse an Array

Discussion in 'Perl Misc' started by Bill H, Feb 3, 2006.

  1. Bill H

    Bill H Guest

    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
     
    Bill H, Feb 3, 2006
    #1
    1. Advertising

  2. Bill H wrote:
    > 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
     
    Jürgen Exner, Feb 3, 2006
    #2
    1. Advertising

  3. Bill H

    Paul Lalli Guest

    Bill H wrote:
    > 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
     
    Paul Lalli, Feb 3, 2006
    #3
  4. Bill H

    Uri Guttman Guest

    >>>>> "BH" == Bill H <> writes:

    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

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Feb 3, 2006
    #4
  5. Bill H

    Bill H Guest

    Jürgen Exner wrote:
    > Bill H wrote:
    > > 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


    Thanks for pointing this out to me, knew there was an easy way to do it.
     
    Bill H, Feb 3, 2006
    #5
  6. "Bill H" <> wrote in
    news::

    > 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

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Feb 3, 2006
    #6
  7. Bill H

    Guest

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


    So has anyone actually let Peter know?
     
    , Feb 3, 2006
    #7
  8. Bill H

    Uri Guttman Guest

    >>>>> "u" == usenet <> writes:

    u> Jürgen Exner wrote:

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


    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


    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Feb 3, 2006
    #8
  9. Bill H

    Bill H Guest

    A. Sinan Unur wrote:
    > "Bill H" <> wrote in
    > news::
    >
    > > 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
    >
    > Sinan
    >
    > --
    > A. Sinan Unur <>
    > (reverse each component and remove .invalid for email address)
    >
    > comp.lang.perl.misc guidelines on the WWW:
    > http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    Bill H, Feb 3, 2006
    #9
  10. "Bill H" <> wrote in message
    news:...
    > 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);


    Congratulations, you've just earned yourself an entry in the Perl SAQ.

    http://www.ginini.com/perlsaq.html
     
    Peter Sundstrom, Feb 3, 2006
    #10
  11. Bill H

    Guest

    Uri Guttman wrote:
    > >>>>> "u" == usenet <> writes:

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

    >
    > 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!
     
    , Feb 3, 2006
    #11
  12. "Uri Guttman" <> wrote in message
    news:...
    > >>>>> "u" == usenet <> writes:

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

    >
    > 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 :)
     
    Peter Sundstrom, Feb 3, 2006
    #12
  13. Bill H

    Paul Lalli Guest

    Peter Sundstrom wrote:
    > http://www.ginini.com/perlsaq.html


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

    Paul Lalli
     
    Paul Lalli, Feb 3, 2006
    #13
  14. wrote in
    news::

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

    --
    A. Sinan Unur <>
    (reverse each component and
    remove .invalid for email address)

    comp.lang.perl.misc guidelines on
    the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Feb 3, 2006
    #14
  15. Bill H

    DJ Stunks Guest

    wrote:
    > <bizarre insane rant snipped>


    someone's off their meds...
     
    DJ Stunks, Feb 3, 2006
    #15
  16. "Paul Lalli" <> wrote in message
    news:...
    > Peter Sundstrom wrote:
    > > http://www.ginini.com/perlsaq.html

    >
    > 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
    ;-)
     
    Peter Sundstrom, Feb 3, 2006
    #16
  17. Bill H

    Paul Lalli Guest

    [OT] HTML entities within <pre> (was: Re: Simple way to Reverse an Array)

    Peter Sundstrom wrote:
    > "Paul Lalli" <> wrote in message
    > news:...
    > > Peter Sundstrom wrote:
    > > > http://www.ginini.com/perlsaq.html

    > >
    > > 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.


    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
     
    Paul Lalli, Feb 3, 2006
    #17
  18. Bill H

    Uri Guttman Guest

    >>>>> "PS" == Peter Sundstrom <> writes:

    PS> "Uri Guttman" <> wrote in message
    PS> news:...
    >> >>>>> "u" == usenet <> writes:

    >>

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

    >>

    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!


    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

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Feb 3, 2006
    #18
  19. Bill H

    Uri Guttman Guest

    Re: [OT] HTML entities within <pre>

    >>>>> "PL" == Paul Lalli <> writes:

    PL> Peter Sundstrom wrote:
    >> >
    >> > 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.


    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

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Feb 3, 2006
    #19
  20. Re: [OT] HTML entities within <pre> (was: Re: Simple way to Reverse an Array)

    "Paul Lalli" <> wrote in message
    news:...
    > Peter Sundstrom wrote:
    > > "Paul Lalli" <> wrote in message
    > > news:...
    > > > Peter Sundstrom wrote:
    > > > > http://www.ginini.com/perlsaq.html
    > > >
    > > > 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.
    >
    > 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.
     
    Peter Sundstrom, Feb 4, 2006
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dogbite
    Replies:
    4
    Views:
    732
    osmium
    Oct 10, 2003
  2. zero

    reverse an array

    zero, Aug 20, 2006, in forum: Java
    Replies:
    4
    Views:
    4,091
    Roland de Ruiter
    Aug 21, 2006
  3. Darshan
    Replies:
    5
    Views:
    1,311
    terminator
    Sep 22, 2009
  4. david.karr
    Replies:
    23
    Views:
    2,205
    Arne Vajhøj
    Jan 3, 2010
  5. Lloyd Zusman
    Replies:
    6
    Views:
    220
    David A. Black
    Aug 4, 2004
Loading...

Share This Page