For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > May 2006 > Printing array from index 1 to last element









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Printing array from index 1 to last element
vincente13@gmail.com

2006-05-24, 4:05 am

Hi all,


foreach my $title (@title) {
print $title
}

This would print all the titles in the array @title.

How can i print the elements from 1 to the last element in the array
using this foreach syntax.

Thanks

John W. Krahn

2006-05-24, 4:05 am

vincente13@gmail.com wrote:
>
> foreach my $title (@title) {
> print $title
> }
>
> This would print all the titles in the array @title.


Or you could just do it like this:

print @title;

> How can i print the elements from 1 to the last element in the array
> using this foreach syntax.


foreach my $title ( @title[ 1 .. $#title ] ) {
print $title;
}


John
--
use Perl;
program
fulfillment
SSS

2006-05-24, 4:05 am

vincente13@gmail.com wrote:
> Hi all,
>
>
> foreach my $title (@title) {
> print $title
> }
>
> This would print all the titles in the array @title.
>
> How can i print the elements from 1 to the last element in the array
> using this foreach syntax.
>
> Thanks
>


You could use shift to remove element [0] from @title before the foreach
loop.
vincente13@gmail.com

2006-05-24, 4:05 am

i guess i settle to some syntax tt im used to

for ( my $i = 1; $i<@title; $i++ ){
print @title[$i];
}

vincente13@gmail.com

2006-05-24, 4:05 am

rather new to perl

and indeed as what many says.there are many ways in doing


appreciate the help!

Brian McCauley

2006-05-24, 4:05 am

vincente13@gmail.com wrote:
> i guess i settle to some syntax tt im used to
>
> for ( my $i = 1; $i<@title; $i++ ){
> print @title[$i];
> }


If you really want to have a subscript variable the for() is still more
Perl-ish as...

for my $i ( 1 .. $#title ) {

Tad McClellan

2006-05-24, 4:05 am

vincente13@gmail.com <vincente13@gmail.com> wrote:
> i guess i settle to some syntax tt im used to
>
> for ( my $i = 1; $i<@title; $i++ ){



foreach my $i ( 1 .. $#title ) {


> print @title[$i];



You should always enable warnings when developing Perl code!


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
Daniel Fischer

2006-05-24, 8:02 am

vincente13!

> How can i print the elements from 1 to the last element in the array
> using this foreach syntax.


foreach my $title (@title[1..@title-1]) {
print $title
}


Daniel

David H. Adler

2006-05-24, 7:02 pm

On 2006-05-24, John W. Krahn <someone@example.com> wrote:
> vincente13@gmail.com wrote:
>
> Or you could just do it like this:
>
> print @title;


Not quite the same. That puts the value of $" in between the elements,
the loop above just jams them all together.

dha

--
David H. Adler - <dha@panix.com> - http://www.panix.com/~dha/
Why *isn't* there a Widget::Gonzo module?
news reader

2006-05-24, 7:02 pm

Hi Brian,

I'ts more perlish, but not the best idea if the array is very big:

AFAIK perl would first construct a list with all numbers from (1 to
$#title-1) and then use these values as an index for the existing array.

I don't know what would run faster but I try to avoid such constructs
since I had once a loop with very big numbers and I ran out of memory.



bye


N

Brian McCauley wrote:
> vincente13@gmail.com wrote:
>
>
>
> If you really want to have a subscript variable the for() is still more
> Perl-ish as...
>
> for my $i ( 1 .. $#title ) {
>

John W. Krahn

2006-05-24, 7:02 pm

David H. Adler wrote:
> On 2006-05-24, John W. Krahn <someone@example.com> wrote:
>
> Not quite the same. That puts the value of $" in between the elements,
> the loop above just jams them all together.


You mean the value of $, and not the value of $" and don't forget that the
value of $\ is appended to every element in the loop.



John
--
use Perl;
program
fulfillment
Tad McClellan

2006-05-24, 7:02 pm

David H. Adler <dha@panix.com> wrote:
> On 2006-05-24, John W. Krahn <someone@example.com> wrote:
>
> Not quite the same.



Huh?


> That puts the value of $" in between the elements,
> the loop above just jams them all together.



Are you seeing quotes that are not there?

Or did you mean $, rather than $" ?


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
Ben Morrow

2006-05-24, 7:02 pm

[quoting fixed: please don't do that]

Quoth news reader <news1234@free.fr>:
> Brian McCauley wrote:
> I'ts more perlish, but not the best idea if the array is very big:
>
> AFAIK perl would first construct a list with all numbers from (1 to
> $#title-1) and then use these values as an index for the existing array.
>
> I don't know what would run faster but I try to avoid such constructs
> since I had once a loop with very big numbers and I ran out of memory.


This used to be the case, but isn't with modern versions of perl. Perl
now optimises a for (1..N) loop to a simple iteration.

Ben

--
It will be seen that the Erwhonians are a m and long-suffering people,
easily led by the nose, and quick to offer up common sense at the shrine of
logic, when a philosopher convinces them that their institutions are not based
on the strictest morality. [Samuel Butler, paraphrased]benmorrow@tiscali.co.uk
Dr.Ruud

2006-05-24, 7:02 pm

David H. Adler schreef:
> John W. Krahn:
[color=darkred]
>
> Not quite the same. That puts the value of $" in between the elements,
> the loop above just jams them all together.



No, $" works with

print "@title";

See $, and $" in perlvar.


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

@_ = (1, 2, 3) ;

print '1a:', @_, "\n" ;
print '1b:', "@_", "\n" ;

$, = ", " ;
$" = " / " ;

print '2a:', @_, "\n" ;
print '2b:', "@_", "\n" ;
__END__

1a:123
1b:1 2 3
2a:, 1, 2, 3,
2b:, 1 / 2 / 3,

--
Affijn, Ruud

"Gewoon is een tijger."


David H. Adler

2006-05-25, 4:01 am

On 2006-05-24, Tad McClellan <tadmc@augustmail.com> wrote:
> David H. Adler <dha@panix.com> wrote:
>
>
> Huh?
>
>
>
>
> Are you seeing quotes that are not there?


Yes, I was. I am filled with shame. *bows head*

Maybe it's these new glasses...

My apologies.

dha

--
David H. Adler - <dha@panix.com> - http://www.panix.com/~dha/
TINC, fnord, tinc.
Ferry Bolhar

2006-05-29, 4:07 am

> foreach my $title (@title) {
> print $title
> }
>
> This would print all the titles in the array @title.
>
> How can i print the elements from 1 to the last element in the array
> using this foreach syntax.


shift @title; foreach...

Greetings, Ferry
--

Ing. Ferry Bolhar
Municipality of Vienna, Department 14
A-1010 Vienna / AUSTRIA
E-mail: bol@adv.magwien.gv.at


John W. Krahn

2006-05-29, 4:07 am

Ferry Bolhar wrote:
>
> shift @title; foreach...


Well, if you're allowed to modify @title then:

foreach my $title ( splice @title, 1 ) {
print $title
}


John
--
use Perl;
program
fulfillment
Ferry Bolhar

2006-05-29, 7:03 pm

John W. Krahn:

>
> Well, if you're allowed to modify @title then:


Why you shouldn't be allowed to modify @title?
If one actually requires the first element later (the
OP didn't tell about that), he could save it

my $first = shift @title;

but I dont't think that this was an issue here..

> foreach my $title ( splice @title, 1 ) {
> print $title
> }


Yes, this will work too, of course.There are
many solutions, how about this short one:

print grep our $i++, @title;

which doesn't affect @title in any way, if this
behaviour would be required or desired.

Greetings, Ferry

--
Ing. Ferry Bolhar
Municipality of Vienna, Department 14
A-1010 Vienna / AUSTRIA
E-mail: bol@adv.magwien.gv.at


John W. Krahn

2006-05-30, 7:04 pm

Ferry Bolhar wrote:
> John W. Krahn:
>
>
> Why you shouldn't be allowed to modify @title?
> If one actually requires the first element later (the
> OP didn't tell about that), he could save it
>
> my $first = shift @title;
>
> but I dont't think that this was an issue here..
>
>
> Yes, this will work too, of course.There are
> many solutions, how about this short one:
>
> print grep our $i++, @title;
>
> which doesn't affect @title in any way, if this
> behaviour would be required or desired.


That will work if you don't already have a $main::i variable with a non-false
value. This avoids that problem:

print do { my $i; grep $i++, @title };



John
--
use Perl;
program
fulfillment
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com