Printing every element of an array

S

Steve Hémond

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
 
J

James E Keenan

Steve Hémond said:
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
 
T

Tad McClellan

Steve Hémond said:
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];
}
 
B

Bob Walton

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

Steve Hémond

Purl said:
Steve Hémond wrote:

(snipped)




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
 
P

Patriote

Purl Gurl said:
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.
 
M

Michael Budash

[snip useless response to purlgurl]

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

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

John Bokma 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-----
 
P

Patriote

Purl Gurl said:
John Bokma 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.
 
C

Charles DeRykus

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,
 

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,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top