concatenate variables during looping question

L

lance-news

Hey all,

Trying to create a loop and need some help
in concatenation

My current syntax:

$Sheet->Cells(16,2)->{Value} = $Ja13_1;
$Sheet->Cells(16,3)->{Value} = $Ja13_2;
$Sheet->Cells(16,4)->{Value} = $Ja13_3;
$Sheet->Cells(16,5)->{Value} = $Ja13_4;
$Sheet->Cells(16,6)->{Value} = $Ja13_5;
$Sheet->Cells(16,7)->{Value} = $Ja13_6;

$Sheet->Cells(34,2)->{Value} = $Ks13_1;
$Sheet->Cells(34,3)->{Value} = $Ks13_2;
$Sheet->Cells(34,4)->{Value} = $Ks13_3;
$Sheet->Cells(34,5)->{Value} = $Ks13_4;
$Sheet->Cells(34,6)->{Value} = $Ks13_5;
$Sheet->Cells(34,7)->{Value} = $Ks13_6;

What I am trying to do:

for (my $i=2; $i=7; $i++){
$Sheet->Cells(16,$i)->{Value} = "$Ja13_"."$i-1";
}

but doesn't work.



Also, how could I incorporate an array so I could create 2 loops such
that all the lines above compress to one line?

Thanks in advance,

Lance
 
S

Scott Bryce

First, please read the posting guidelines for this group. I was unable
to copy and paste your code and run it. I don't want to take the time to
re-create your script to run it.

lance-news said:
for (my $i=2; $i=7; $i++)

This for loop will run forever. $i=7 will always evaluate to True.

did you mean:

for my $i (2 .. 7)
 
J

James Taylor

lance-news said:
My current syntax:

$Sheet->Cells(16,2)->{Value} = $Ja13_1;
$Sheet->Cells(16,3)->{Value} = $Ja13_2;
$Sheet->Cells(16,4)->{Value} = $Ja13_3;
$Sheet->Cells(16,5)->{Value} = $Ja13_4;
$Sheet->Cells(16,6)->{Value} = $Ja13_5;
$Sheet->Cells(16,7)->{Value} = $Ja13_6;

$Sheet->Cells(34,2)->{Value} = $Ks13_1;
$Sheet->Cells(34,3)->{Value} = $Ks13_2;
$Sheet->Cells(34,4)->{Value} = $Ks13_3;
$Sheet->Cells(34,5)->{Value} = $Ks13_4;
$Sheet->Cells(34,6)->{Value} = $Ks13_5;
$Sheet->Cells(34,7)->{Value} = $Ks13_6;


You should create a hash to hold all those variables instead of having
so many similarly named variables. I hate to imagine what your my()
declarations look like. What? You don't have any? Aren't you using
strict? Tut tut.
What I am trying to do:

for (my $i=2; $i=7; $i++){
$Sheet->Cells(16,$i)->{Value} = "$Ja13_"."$i-1";
}

but doesn't work.

Just saying "doesn't work" is something you should avoid as it's just
not helpful to anyone. Say what it doesn't do and what you expect it to
do and people can help.

If you had some hashes called say %Ja and %Ks to hold all those values,
you could just write:

for (my $i=2; $i < 7; $i++) {
$Sheet->Cells(16, $i + 1)->{Value} = $Ja{"13_$i"};
$Sheet->Cells(34, $i + 1)->{Value} = $Ks{"13_$i"};
}
Also, how could I incorporate an array so I could create 2 loops such
that all the lines above compress to one line?

Not sure what you mean, and I'm not sure the clarity and maintainability
of you code would be served particularly well by compressing into just
one line anyway.
 
J

James Taylor

James Taylor said:
for (my $i=2; $i < 7; $i++) {
$Sheet->Cells(16, $i + 1)->{Value} = $Ja{"13_$i"};
$Sheet->Cells(34, $i + 1)->{Value} = $Ks{"13_$i"};
}

Oops, that should run from $1=1 to 6, of course.
 
S

Scott Bryce

James said:
If you had some hashes called say %Ja and %Ks to hold all those values,
you could just write:

for (my $i=2; $i < 7; $i++) {
$Sheet->Cells(16, $i + 1)->{Value} = $Ja{"13_$i"};
$Sheet->Cells(34, $i + 1)->{Value} = $Ks{"13_$i"};
}

He could write that, but then $Sheet->Cells(16, 2)->{Value} and
$Sheet->Cells(34, 2)->{Value} would never be assigned.

He might prefer to write:

for my $i (2 .. 7)
{
$Sheet->Cells(16, $i)->{Value} = $Ja{'13_' . $i - 1};
$Sheet->Cells(34, $i)->{Value} = $Ks{'13_' . $i - 1};
}

Or he might prefer to use arrays instead of hashes.

my @Ja13;
my @Ks13;

# Assign some values to each array

for my $i (2 .. 7)
{
$Sheet->Cells(16, $i)->{Value} = $Ja13[$i - 1];
$Sheet->Cells(34, $i)->{Value} = $Ks13[$i - 1];
}
 
T

Tad McClellan

lance-news said:
Trying to create a loop and need some help
in concatenation


What you really need is a better choice of data structure.


My current syntax:

$Sheet->Cells(16,2)->{Value} = $Ja13_1;
$Sheet->Cells(16,3)->{Value} = $Ja13_2;
$Sheet->Cells(16,4)->{Value} = $Ja13_3;
$Sheet->Cells(16,5)->{Value} = $Ja13_4;
$Sheet->Cells(16,6)->{Value} = $Ja13_5;
$Sheet->Cells(16,7)->{Value} = $Ja13_6;


Sequentially-named variables are a red flag that an array would
be a better data structure than a slew of individual scalars.

What I am trying to do:

for (my $i=2; $i=7; $i++){


foreach my $i ( 2 .. 7 ) { # easier to read and understand

$Sheet->Cells(16,$i)->{Value} = "$Ja13_"."$i-1";


What you are attempting to do is use something called a "symbolic reference".

They are bad.

They can lead to hard-to-troubleshoot bugs, and can nearly always
be refactored using a hash and/or a real reference.

}

but doesn't work.


Have you seen the Posting Guidelines that are posted here frequently?

Also, how could I incorporate an array so I could create 2 loops such
that all the lines above compress to one line?


If you had @Ja13, then:

$Sheet->Cells(16, $_)->{Value} = $Ja13[$_ - 1] for 2 .. 7;
 
B

burlo.stumproot

lance-news said:
Hey all,

Trying to create a loop and need some help
in concatenation

My current syntax:

$Sheet->Cells(16,2)->{Value} = $Ja13_1;
$Sheet->Cells(16,3)->{Value} = $Ja13_2;
$Sheet->Cells(16,4)->{Value} = $Ja13_3;
$Sheet->Cells(16,5)->{Value} = $Ja13_4;
$Sheet->Cells(16,6)->{Value} = $Ja13_5;
$Sheet->Cells(16,7)->{Value} = $Ja13_6;

$Sheet->Cells(34,2)->{Value} = $Ks13_1;
$Sheet->Cells(34,3)->{Value} = $Ks13_2;
$Sheet->Cells(34,4)->{Value} = $Ks13_3;
$Sheet->Cells(34,5)->{Value} = $Ks13_4;
$Sheet->Cells(34,6)->{Value} = $Ks13_5;
$Sheet->Cells(34,7)->{Value} = $Ks13_6;

You should probably use Range instead.

# write a 2 rows by 3 columns range
$sheet->Range("A8:C9")->{Value} = [[ undef, 'Xyzzy', 'Plugh' ],
[ 42, 'Perl', 3.1415 ]];
 

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,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top