What is the right syntax?

C

Chris Newman

@names = {"a","b","c"};

@occup = {"d","e","f'};

foreach $i @names {

print ("$names, $occup \n");

}

The result I am after follows

a,d
be
cf

I suspect that $i returns the value stored in each element in the array not
the index . How do I change the code to make this happen?

Thanks
 
A

anno4000

Chris Newman said:
@names = {"a","b","c"};

@occup = {"d","e","f'};

foreach $i @names {

That doesn't compile. Please don't present code with syntax errors,
you can easily avoid that.
print ("$names, $occup \n");

}

The result I am after follows

a,d
be
cf

I suspect that $i returns the value stored in each element in the array not
the index . How do I change the code to make this happen?


One way is the module List::MoreUtils (from CPAN). The function
pairwise() does exactly what you want.

Anno
 
M

Michael Meissner

Chris Newman said:
@names = {"a","b","c"};

@occup = {"d","e","f'};

foreach $i @names {

print ("$names, $occup \n");

}

The result I am after follows

a,d
be
cf

I suspect that $i returns the value stored in each element in the array not
the index . How do I change the code to make this happen?


You don't want $i to be the elements in @names, but an index:

@names = {"a", "b", "c"};
@occup = {"d", "e", "f"};
for ($i = 0; $i <= $#names; $i++) {
print $names[$i], $occup[$i], "\n";
}

However you might want to restructure the app to use more complicated data
structures, rather than parallel arrays:

@db = ( { "names" => "a", "occup" => "d" },
{ "names" => "b", "occup" => "e" },
{ "names" => "c", "occup" => "f" } );

foreach $i (0..$#db) {
print $db[$i]->{"names"}, $db[$i]->{"occup"}, "\n";
}

or:

@db = ( { "names" => "a", "occup" => "d" },
{ "names" => "b", "occup" => "e" },
{ "names" => "c", "occup" => "f" } );

foreach $i (@db) {
print $i->{"names"}, $i->{"occup"}, "\n";
}
 
J

Jürgen Exner

Michael said:
You don't want $i to be the elements in @names, but an index:

@names = {"a", "b", "c"};
@occup = {"d", "e", "f"};
for ($i = 0; $i <= $#names; $i++) {

Why not
for $i (0..$#names) {
print $names[$i], $occup[$i], "\n";
}

jue
 
C

Chris Newman

Chris Newman ([email protected]) wrote on MMMMDCCXII September
MCMXCIII in <URL:;;
;; @names = {"a","b","c"};
;;
;; @occup = {"d","e","f'};
;;
;; foreach $i @names {
;;
;; print ("$names, $occup \n");
;;
;; }
;;
;; The result I am after follows
;;
;; a,d
;; be
;; cf
;;
;; I suspect that $i returns the value stored in each element in the array
not
;; the index . How do I change the code to make this happen?


my @names = qw [a b c];
my @occup = qw [d e f];

for (my $i = 0; $i < @names; $i ++) {
printf "%s %s\n", $names [$i], $occup [$i];
}


Abigail


thanks again people must appreciated :)
 
T

Tad McClellan

Chris Newman said:
foreach $i @names {
^^^^^^
^^^^^^ no parenthesis
print ("$names, $occup \n");

^ ^
^ ^ no dollar signs

That is not Perl code.

How do I change the code to make this happen?


This is the Perl newsgroup.

If you want to discuss it _here_, then the first change should be
to make it into Perl code.
 

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,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top