Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Printing every element of an array

Reply
Thread Tools

Printing every element of an array

 
 
Steve Hémond
Guest
Posts: n/a
 
      09-29-2003
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
 
Reply With Quote
 
 
 
 
James E Keenan
Guest
Posts: n/a
 
      09-29-2003

"Steve Hémond" <(E-Mail Removed)> wrote in message
news:CMLdb.3506$(E-Mail Removed).. .
> 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


 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      09-29-2003
Steve Hémond <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Bob Walton
Guest
Posts: n/a
 
      09-29-2003
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

 
Reply With Quote
 
Steve Hémond
Guest
Posts: n/a
 
      09-29-2003
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
 
Reply With Quote
 
Patriote
Guest
Posts: n/a
 
      09-29-2003
Purl Gurl <(E-Mail Removed)> lectured in message
news:<(E-Mail Removed)>...
> 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.
 
Reply With Quote
 
Michael Budash
Guest
Posts: n/a
 
      09-29-2003
In article <(E-Mail Removed)> ,
(E-Mail Removed) (Patriote) wrote:

> Purl Gurl <(E-Mail Removed)> lectured in message
> news:<(E-Mail Removed)>...
> > Steve Hémond wrote:


[snip useless response to purlgurl]

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

--
Michael Budash
 
Reply With Quote
 
Eric J. Roode
Guest
Posts: n/a
 
      09-30-2003
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Purl Gurl <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> 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-----
 
Reply With Quote
 
Patriote
Guest
Posts: n/a
 
      09-30-2003
Purl Gurl <(E-Mail Removed)> extrolled in message
news:<(E-Mail Removed)>...
> 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.
 
Reply With Quote
 
Charles DeRykus
Guest
Posts: n/a
 
      09-30-2003
In article <CMLdb.3506$(E-Mail Removed)>,
Steve =?ISO-8859-1?Q?H=E9mond?= <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to Update/insert an xml element's text----> (<element>text</element>) HANM XML 2 01-29-2008 03:31 PM
Explorer.exe error report every time access every type of video me =?Utf-8?B?SXJ3YW5zeWFo?= ASP .Net 4 10-30-2007 07:49 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-18-2007 10:11 AM
extract every other element from an array kg.google@olympiakos.com Perl Misc 10 09-02-2005 05:50 PM
how does one get every element in a window into an array? lawrence Javascript 2 11-22-2004 02:55 AM



Advertisments