Printing array from index 1 to last element

V

vincente13

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
 
J

John W. Krahn

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
 
S

SSS

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

vincente13

i guess i settle to some syntax tt im used to

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

vincente13

rather new to perl

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


appreciate the help!
 
B

Brian McCauley

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 ) {
 
D

Daniel Fischer

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
 
D

David H. Adler

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
 
N

news reader

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 said:
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 ) {
 
B

Ben Morrow

[quoting fixed: please don't do that]

Quoth news reader said:
Brian said:
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 ) {
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
 
J

John W. Krahn

David said:
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
 
T

Tad McClellan

David H. Adler said:
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 $" ?
 
D

Dr.Ruud

David H. Adler schreef:
John W. Krahn:

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

John W. Krahn

Ferry said:
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
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top