Pointer to array?

V

Vito Corleone

I have an 'if' inside a 'for' like this:

--- cut ---
for (my $x = 0; $x < 100; $x++) {
if ($status eq "demo") {
$link = "demo.html";
}
else {
$link = $row[$x]->{link};
}
print $link;
}
--- cut ---

How can I take the 'if' out from the loop? I want to make it looks like
this:

--- cut ---
if ($status eq "demo") {
$link = "demo.html";
}
else {
$link = ???? ## Here I don't know
## I want something like '$row[$x]->{link}'
}

for (my $x = 0; $x < 100; $x++) {
print $link; ## Should print "demo.html"
## Or the value of $row[$x]->{link}
}

How can I do this?

Regards,
Vito
 
G

gnari

Vito Corleone said:
I have an 'if' inside a 'for' like this:

--- cut ---
for (my $x = 0; $x < 100; $x++) {
if ($status eq "demo") {
$link = "demo.html";
}
else {
$link = $row[$x]->{link};
}
print $link;
}
--- cut ---

How can I take the 'if' out from the loop?

you do not explain why.
what is wrong with it as it is ?

if you just dont like the look of the if,
you might prefer:
$link=($status eq 'demo')?$link:$row[$x]->{link};

if your data structure allows it, you might:
if ($status eq "demo") {
$row[$_]->{link} = "demo.html" for (0..99);
}
for (my $x = 0; $x < 100; $x++) {
print $row[$_]->{link};
}

but I do not know what the gain would be.

maybe you are having a X-Y problem:
the problem is something else, but
you *think* that it will be solved by changing your
loop, so you ask about that, without telling us
the real problem.

gnari
 
J

John Bokma

Vito said:
I have an 'if' inside a 'for' like this:

--- cut ---
for (my $x = 0; $x < 100; $x++) {
if ($status eq "demo") {
$link = "demo.html";
}
else {
$link = $row[$x]->{link};
}
print $link;
}
--- cut ---

How can I take the 'if' out from the loop? I want to make it looks like
this:

--- cut ---
if ($status eq "demo") {
$link = "demo.html";
}
else {
$link = ???? ## Here I don't know
## I want something like '$row[$x]->{link}'
}

for (my $x = 0; $x < 100; $x++) {
print $link; ## Should print "demo.html"
## Or the value of $row[$x]->{link}
}

How can I do this?

if ($status eq 'demo') {
print 'demo.html' x 100;
} else {
for (my $x = 0; $x < 100; $x++) {
print $row[$x]->{link};
}
}

Better to put the 100 in a constant
 
J

Joe Smith

Vito said:
$link = ???? ## Here I don't know
## I want something like '$row[$x]->{link}'

No, that can only be done inside the for() loop.

You could use 'if' to bypass the loop.

if ($status eq "demo) {
print "demo.html" for (0..99);
} else {
print $row[$_]->{link} for (0..99);
}

-Joe
 
J

Joe Smith

Vito said:
I have an 'if' inside a 'for' like this:

--- cut ---
for (my $x = 0; $x < 100; $x++) {
if ($status eq "demo") {
$link = "demo.html";
}
else {
$link = $row[$x]->{link};
}
print $link;
}
--- cut ---

How can I take the 'if' out from the loop?

Have you considered using a reference to a function?

my $subr;
if ($status eq "demo") {
$subr = sub { "demo.html"; };
} else {
$subr = sub { my $x = shift; $row[$x]->{link}; }
}
for my $y (0..99) {
print $subr->($y);
}

-Joe
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,076
Latest member
OrderKetoBeez

Latest Threads

Top