Help - Search - Member List - Calendar
Full Version: Shortest iteration of an array?
WorkTheWeb Forums > Webmaster Resources > Perl Beginner Help
Support our Sponsors!
Angerstein
for ($y = 1; $y <= $thrc; $y++){
$ary[$y] = threads->new(&net1, $proto, $time);
$ary[$y]->detach;
}

#or

@ary[1..150] = 0;
foreach $element (@ary) {
$element = threads->new(&net1, $proto, $time);
$element = detach;
}

something short like or similar possible?

#this dont work...
@ary[1..150] = threads->new(&net1, $proto, $time);
@ary[1..150] = detach;

Jay Savage
On 7/6/05, Angerstein <[Email Removed]> wrote:
QUOTE


for ($y = 1; $y <= $thrc; $y++){
$ary[$y] = threads->new(&net1, $proto, $time);
$ary[$y]->detach;
}

#or

@ary[1..150] = 0;
foreach $element (@ary) {
$element = threads->new(&net1, $proto, $time);
$element = detach;
}

something short like or similar possible?

#this dont work...
@ary[1..150] = threads->new(&net1, $proto, $time);
@ary[1..150] = detach;



Angerstein,

Not sure what you mean by "short" here. Do you want the fewest lines
of code? Quickest execution? Least typing? there are a lot of
different kinds of "short" in programming.

I'm not quite sure what you're after with '@ary[1..150]'. This is
slice notation, and it probably doesn't do what you think it does. It
doesn't create an array with 150 elements. It takes elements 1-150 of
a prexisting array and does something with them. In a foreach loop,
saying 'foreach $element (@ary[1.150])' is equivalent to saying:

foreach (1..150) {
$element = @ary[$_];
}

You're probably looking for something like the following:

foreach (1..150) {
@ary[$_] = threads->new(&net1, $proto, $time);
@ary[$_]->detatch;
}

or even

map {@ary[$_] = threads->new(&net1, $proto, $time); @ary[$_]->detach} 1..150;

I'm not sure, though, why you're bothring with the array if you're
just going to detatch. I think a little code would help us understand
better what it is you're trying to do.

-- jay
--------------------------------------------------
This email and attachment(s): [ ] blogable; [ x ] ask first; [ ]
private and confidential

daggerquill [at] gmail [dot] com
http://www.tuaw.com http://www.dpguru.com http://www.engatiki.org


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2005 Invision Power Services, Inc.