Ryan Moszynski am Montag, 24. Juli 2006 18:40:
> Is there a way to make my commented 'foreach" line act the same as the
> line above it?

Yes!

DISCLAIMER: Please excuse my bad english

> Can I pass a list as a variable as I am trying to do, or doesn't perl
> support that?

You can, but... see below.

> ###########
> #!/usr/bin/perl -w
> $|=1;

I don't think setting the autoflush to true is necessary, since you're 
interested in printing line after line.

> #use strict;

Why comment this out?

> system "clear";
> my @array = 1024;

This will create a list with one element, $array[0], containing 1024.

You don't need to "dim" an array in perl; perl expands it automatically and 
fills the gaps with an undef value (see the script at the end)

> my $list4 = "0..10,33..43,100..111";

You assign a simple string to the scalar variable $list4, that's why you can't 
loop over it, so:

my @list4=(0..10,33..43,100..111);

>     foreach (0..10,33..43,100..111){
>     #foreach ($list4){

foreach (@list4) {

>     $array[$_] = $_;
>
>     print "array--$array[$_]-- --$_--\n";
>
> }
> ###########

Have a look into the perl data types documentation:

perldoc perldata



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

my @array;
my @list4=(0..10,33..43,100..111);

foreach (@list4) {
  $array[$_] = $_;
  print "array--$array[$_]-- --$_--\n";
}

# look into the data structure:
use Data::Dumper 'Dumper';
warn Dumper [EMAIL PROTECTED];

__END__

Dani

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to