Home > Archive > PERL Miscellaneous > January 2006 > concatenate variables during looping question
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
concatenate variables during looping question
|
|
| lance-news 2006-01-25, 7:03 pm |
| 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
| |
| Scott Bryce 2006-01-25, 7:03 pm |
| 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 wrote:
> 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)
| |
| James Taylor 2006-01-25, 7:03 pm |
| lance-news <lance-news@augustmail.com> wrote:
> 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.
--
James Taylor
| |
| James Taylor 2006-01-25, 7:03 pm |
| James Taylor <usenet@oakseed.demon.co.uk.invalid> wrote:
> 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.
--
James Taylor
| |
| Scott Bryce 2006-01-25, 7:03 pm |
| James Taylor wrote:
> 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];
}
| |
| Jim Gibson 2006-01-25, 7:03 pm |
| In article <43d7b145$0$1765$8b463f8a@news.nationwide.net>, lance-news
<lance-news@augustmail.com> wrote:
> 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.
See perldoc -q "variable as a variable name" for how to do it and why
you shouldn't.
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
| |
| Tad McClellan 2006-01-26, 3:58 am |
| lance-news <lance-news@augustmail.com> wrote:
> 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;
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
| |
| burlo.stumproot@gmail.com 2006-01-29, 6:59 pm |
| lance-news <lance-news@augustmail.com> writes:
> 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 ]];
> Thanks in advance,
>
> Lance
|
|
|
|
|