Printing every element of an array

Discussion in 'Perl Misc' started by Steve Hémond, Sep 29, 2003.

  1. Hi people,

    This is actually a basic question ...

    For each element of an array, I need to print the previous element, the
    actual element, and the next element on a line.

    Example :

    @test = "One","Two","Three","Four","Five";

    The program need to read each array element, then print its previous, actual
    and next value :

    Output :

    *nothing*,One,Two
    One, Two, Three
    Two, Three, Four
    Three,Four,Five

    I was thinking of using a simple FOR statement :

    for ($i=$array[0]; $i==$array[LAST ELEMENT]; $i++)

    I know this is completely wrong. I just want to show you the logic I am
    thinking about.

    Any ideas ?

    Thanks in advance,

    Steve
    Steve Hémond, Sep 29, 2003
    #1
    1. Advertising

  2. "Steve Hémond" <> wrote in message
    news:CMLdb.3506$...
    > Hi people,
    >
    > This is actually a basic question ...
    >
    > For each element of an array, I need to print the previous element, the
    > actual element, and the next element on a line.
    >
    > Example :
    >
    > @test = "One","Two","Three","Four","Five";
    >
    > The program need to read each array element, then print its previous,

    actual
    > and next value :
    >
    > Output :
    >
    > *nothing*,One,Two
    > One, Two, Three
    > Two, Three, Four
    > Three,Four,Five
    >


    Not elegant, but it works:

    my @test = qw(One Two Three Four Five);

    for (my $i=0; $i<=$#test; $i++) {
    if ($i == 0) {
    print "Nothing\t$test[$i]\t$test[$i+1]\n";
    } elsif ($i == $#test) {
    print "$test[$i-1]\t$test[$i]\tNothing\n";
    } else {
    print "$test[$i-1]\t$test[$i]\t$test[$i+1]\n";
    }
    }

    jimk
    James E Keenan, Sep 29, 2003
    #2
    1. Advertising

  3. Steve Hémond <> wrote:

    > Example :
    >
    > @test = "One","Two","Three","Four","Five";



    That has the same effect as:

    $test[0] = 'One';
    or
    @test = 'One';
    or
    @test = ('One');

    Please post actual code!


    > The program need to read each array element, then print its previous, actual
    > and next value :



    --------------------------------
    #!/usr/bin/perl
    use strict;
    use warnings;

    my @test = ("One","Two","Three","Four","Five");

    my $prev='*nothing*';
    for my $i ( 0.. $#test ) {
    print "$prev,$test[$i],$test[$i+1]\n";
    $prev = $test[$i];
    }
    --------------------------------


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Sep 29, 2003
    #3
  4. Steve Hémond

    Bob Walton Guest

    Steve Hémond wrote:

    ....
    > For each element of an array, I need to print the previous element, the
    > actual element, and the next element on a line.
    >
    > Example :
    >
    > @test = "One","Two","Three","Four","Five";


    ---------^---------------------------------^---need =(....);


    >
    > The program need to read each array element, then print its previous, actual
    > and next value :
    >
    > Output :
    >
    > *nothing*,One,Two
    > One, Two, Three
    > Two, Three, Four
    > Three,Four,Five
    >
    > I was thinking of using a simple FOR statement :
    >
    > for ($i=$array[0]; $i==$array[LAST ELEMENT]; $i++)
    >
    > I know this is completely wrong. I just want to show you the logic I am
    > thinking about.
    >
    > Any ideas ?

    ....


    > Steve


    Maybe something like:

    @test =("One","Two","Three","Four","Five");
    print join(',',('*nothing*',@test,'*nothing*')
    [$_..$_+2])."\n" for 0..$#test;

    --
    Bob Walton
    Email: http://bwalton.com/cgi-bin/emailbob.pl
    Bob Walton, Sep 29, 2003
    #4
  5. Purl Gurl wrote:

    > Steve Hémond wrote:
    >
    > (snipped)
    >
    >> Example :

    >
    >> @test = "One","Two","Three","Four","Five";

    >
    >
    > How about you do this. First write legitimate Perl code,
    > then revisit here to ask questions, to post comments,
    > or to troll.
    >
    > #!perl
    >
    > @test = "One","Two","Three","Four","Five";
    >
    > for (@test)
    > { print "Element: $_\n"; }
    >
    >
    > PRINTED RESULTS:
    > ________________
    >
    > Element: One
    >
    >
    >
    > Purl Gurl


    Thanks Purl Gurl, I am new to Perl and following a quick tutorial.. I just
    forgot to paste my code. Sorry about that. After this tutorial I will start
    reading the Perl book I just bought yesterday.

    Regards,

    Steve
    Steve Hémond, Sep 29, 2003
    #5
  6. Steve Hémond

    Patriote Guest

    Purl Gurl <> lectured in message
    news:<>...
    > Steve Hémond wrote:
    >
    > > Purl Gurl wrote:
    > > > Steve Hémond wrote:

    >
    > (snipped)
    >
    > > > How about you do this. First write legitimate Perl code,

    >
    > > I just forgot to paste my code.

    >
    >
    > Your previous articles do not exhibit this problem.
    >
    > Others here do not hold expectations of posters.
    > I have high expectations of posters. Any article


    So do I. I expect old-timers like you not to bore us to death. Yawn.

    > I read, any article to which I respond, makes use
    > of time out of my life.
    >
    > Each article I open, perhaps read, uses precious
    > time, my time. Most know I become rather annoyed


    As is the time I lie awake with insomnia precious. Therefore, I will
    again read your little lecture here tonight should I have insomnia.

    > by those who waste my time with poorly written
    > articles. I have expectations. Others do not.


    Others actually have expectations that you show some friendliness
    towards "newcomers" and post a useful response, with possibly a short
    admonition at the end to train the "dumb puppies". Does it really pay
    to scare people off? What is this group for except to answer
    questions anyway? If you want a venue for esoteric, elitist
    discussions, hook up with a private server somewhere.

    > Post good quality articles and you will receive
    > good quality responses, as a general rule. If not,


    Please don't tell me this is a quality response. Anyway, climb off
    the guy. He was apologetic. I'm tired of the snotty lectures in this
    group that often consume more resources than the dumb newcomer
    questions. Either you petition to add "advanced" to this group name,
    or you tolerate the noise of newcomers. In other words, punish them
    gently after you have answered their question. Or, how about this -
    save your precious time by not even joining threads that are insulting
    to your intelligence.

    > you only add to the noise in this trollfest group.


    Incessant lectures like yours qualify as noise.
    Patriote, Sep 29, 2003
    #6
  7. In article <>,
    (Patriote) wrote:

    > Purl Gurl <> lectured in message
    > news:<>...
    > > Steve Hémond wrote:


    [snip useless response to purlgurl]

    please do NOT feed the troll! it only keeps it alive...

    --
    Michael Budash
    Michael Budash, Sep 29, 2003
    #7
  8. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Purl Gurl <> wrote in
    news::

    > John Bokma trolled:
    >
    >> Patriote trolled:

    > (Michael Budash trolled)


    Your accusations of trolling are also getting old.


    > My presumption is, the three of you boys,


    Your sexist accusations are also getting old.


    John was right. Your posts are simply boring. It's the same tired old
    tripe, over and over again.

    - --
    Eric
    $_ = reverse sort $ /. r , qw p ekca lre uJ reh
    ts p , map $ _. $ " , qw e p h tona e and print

    -----BEGIN PGP SIGNATURE-----
    Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

    iQA/AwUBP3lsFWPeouIeTNHoEQKwdACfXYfuX8YNQ/uFBccK6rCaItz2Om4AoLC9
    W5irpHszwKLsKuz8lKbC5pwq
    =Lw3c
    -----END PGP SIGNATURE-----
    Eric J. Roode, Sep 30, 2003
    #8
  9. Steve Hémond

    Patriote Guest

    Purl Gurl <> extrolled in message
    news:<>...
    > John Bokma trolled:
    >
    > > Patriote trolled:

    > (Michael Budash trolled)
    >
    > (snipped)
    >
    > My presumption is, the three of you boys, "Patriote,"
    > "Michael Budash" and yourself, do not support nor
    > adhere to these illegitimate posting guidelines
    > presented here weekly.


    Now how do you even know I'm a boy?


    > An additional valid presumption is you three boys
    > do support posting troll articles, as all three
    > of you have done, repeatedly over time.


    Incorrect. This is my second posting to this group ever. The first
    was my response to your lecture of a couple days ago.

    > Quite the coincidence all three of "you" would
    > post troll articles within a very short time
    > frame; within minutes of each "other."



    Well, I could complain about the "coincidence" that I started
    receiving virus-mail sent to my fastmail.fm address the day I posted
    my rant, never having received any such mail before. But, that would
    be silly, and somebody would point out it was probably the result of
    some newsgroup crawler finding a new email address in USENET.

    My whole point of this was that posting guidelines are great, but
    newcomers should be reminded with a brief warning. You don't train a
    dog in paragraphs. I completely understand the need to preserve this
    group and maintain its usefulness. And, I disagree somewhat with an
    earlier posting about USENET. It certainly does function as a help
    desk in some regards. I've gotten more help from USENET than any
    other source, period. This has been because of the generosity and
    patience of USENET contributors, of course. Its sole purpose is not
    that of a help desk, admittedly.
    Patriote, Sep 30, 2003
    #9
  10. In article <CMLdb.3506$>,
    Steve =?ISO-8859-1?Q?H=E9mond?= <> wrote:
    >Hi people,
    >
    >This is actually a basic question ...
    >
    >For each element of an array, I need to print the previous element, the
    >actual element, and the next element on a line.
    >
    >Example :
    >
    >@test = "One","Two","Three","Four","Five";
    >
    >The program need to read each array element, then print its previous, actual
    >and next value :
    >
    >Output :
    >
    >*nothing*,One,Two
    >One, Two, Three
    >Two, Three, Four
    >Three,Four,Five
    >
    >I was thinking of using a simple FOR statement :
    >
    >for ($i=$array[0]; $i==$array[LAST ELEMENT]; $i++)
    >


    If the array's not too humongous to slow things down,
    you could do:

    { my @t= ("nothing",@test,"nothing");
    print join "," => @t[$_-1..$_+1] for 1..$#t-1; }

    HTH,
    --
    Charles DeRykus
    Charles DeRykus, Sep 30, 2003
    #10
    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. Mokkapati

    Printing files every step

    Mokkapati, May 18, 2006, in forum: C Programming
    Replies:
    35
    Views:
    837
    Richard Heathfield
    May 20, 2006
  2. =?Utf-8?B?SXJ3YW5zeWFo?=
    Replies:
    4
    Views:
    2,451
    =?Utf-8?B?SXJ3YW5zeWFo?=
    Oct 30, 2007
  3. HANM
    Replies:
    2
    Views:
    709
    Joseph Kesselman
    Jan 29, 2008
  4. lawrence
    Replies:
    2
    Views:
    68
    Rob B
    Nov 22, 2004
  5. Replies:
    10
    Views:
    1,374
Loading...

Share This Page