|
Posted by Rose on March 5, 2008, 10:37 pm
Please log in for more thread options
I have to add objects dynamically in a code and therefore the number of
objects are not known beforehand. How to achieve this effectly in a simple
way? e.g.
#fixed codes
$panel = Panel->new(
-length => 1000,
-width => 10,
);
#dynamic codes
my $obj1 = Object::Generic->new(
-start => 10,
-end => 10,
-display_name => 'C'
);
my $obj2 = Object::Generic->new(
-start => 88,
-end => 89,
-display_name => 'T'
);
...
my $objn = Object::Generic->new(
-start => p,
-end => q,
-display_name => 'N'
);
$panel->add_obj($obj1);
$panel->add_obj($obj2);
...
$panel->add_obj($objn);
|
|
Posted by Frank Seitz on March 5, 2008, 11:18 pm
Please log in for more thread options
Rose wrote:
> I have to add objects dynamically in a code and therefore the number of
> objects are not known beforehand. How to achieve this effectly in a simple
> way? e.g.
my $panel = Panel->new(...);
my @arr = ([10,10,'C'],[88,89,'T'],...);
for my $e (@arr) {
my $obj = Object::Generic->new(
-start=>$e->[0],
-end=>$e->[1],
-display_name=>$e->[2],
);
$panel->add_obj($obj);
}
Frank
--
Dipl.-Inform. Frank Seitz; http://www.fseitz.de/ Anwendungen für Ihr Internet und Intranet
Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel
|
|
Posted by Rose on March 6, 2008, 12:16 am
Please log in for more thread options
> Rose wrote:
>> I have to add objects dynamically in a code and therefore the number of
>> objects are not known beforehand. How to achieve this effectly in a
>> simple
>> way? e.g.
>
> my $panel = Panel->new(...);
>
> my @arr = ([10,10,'C'],[88,89,'T'],...);
> for my $e (@arr) {
> my $obj = Object::Generic->new(
> -start=>$e->[0],
> -end=>$e->[1],
> -display_name=>$e->[2],
> );
> $panel->add_obj($obj);
> }
>
> Frank
> --
> Dipl.-Inform. Frank Seitz; http://www.fseitz.de/
> Anwendungen für Ihr Internet und Intranet
> Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel
Frank, Thanks a lot for your response. Indeed, @arr is not known beforehand
and the content of @arr is generated by another perlscript. How would you
recommend to bridge these 2 perlscripts? The first one, I store the 10, 88,
...; 10, 89, ...; and C, T, ... into separate arrays, say @a1, @a2, @a3. A
simple but dirty way is to copy the contents of the 1st file to the 2nd and
then @arr = (@a1, @a2, @a3), but this does not look to be a good practice.
|
|
Posted by Frank Seitz on March 6, 2008, 12:43 am
Please log in for more thread options Rose wrote:
>
> Frank, Thanks a lot for your response. Indeed, @arr is not known beforehand
> and the content of @arr is generated by another perlscript. How would you
> recommend to bridge these 2 perlscripts? The first one, I store the 10, 88,
> ...; 10, 89, ...; and C, T, ... into separate arrays, say @a1, @a2, @a3.
> A simple but dirty way is to copy the contents of the 1st file to the 2nd and
> then @arr = (@a1, @a2, @a3), but this does not look to be a good practice.
To copy @a1, @a2, @a3 this way would not work.
Copying is not necessary. Try this:
for (my $i = 0; $i < @a1; $i++) {
my $obj = Object::Generic->new(
-start=>$a1[$i],
-end=>$a2[$i],
-display_name=>$a3[$i],
);
$panel->add_obj($obj);
}
Frank
--
Dipl.-Inform. Frank Seitz; http://www.fseitz.de/ Anwendungen für Ihr Internet und Intranet
Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel
|
|
Posted by Rose on March 6, 2008, 3:19 am
Please log in for more thread options > Rose wrote:
>>
>> Frank, Thanks a lot for your response. Indeed, @arr is not known
>> beforehand
>> and the content of @arr is generated by another perlscript. How would you
>> recommend to bridge these 2 perlscripts? The first one, I store the 10,
>> 88,
>> ...; 10, 89, ...; and C, T, ... into separate arrays, say @a1, @a2, @a3.
>> A simple but dirty way is to copy the contents of the 1st file to the 2nd
>> and
>> then @arr = (@a1, @a2, @a3), but this does not look to be a good
>> practice.
>
> To copy @a1, @a2, @a3 this way would not work.
> Copying is not necessary. Try this:
>
> for (my $i = 0; $i < @a1; $i++) {
> my $obj = Object::Generic->new(
> -start=>$a1[$i],
> -end=>$a2[$i],
> -display_name=>$a3[$i],
> );
> $panel->add_obj($obj);
> }
>
> Frank
> --
> Dipl.-Inform. Frank Seitz; http://www.fseitz.de/
> Anwendungen für Ihr Internet und Intranet
> Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel
But can I use a for loop to achieve the following effect? I guess I can't
simply code:
$panel->add_track([@obj],
-label => 1,
);
$panel->add_track([$obj1,$obj2,$obj3, ..., $objn],
-label => 1,
);
|
| Similar Threads | Posted | | how to "see" DOS errorlevel codes? | January 15, 2007, 12:17 pm |
| Help writing semaphore codes | July 12, 2007, 5:27 pm |
| Help: Debug perl codes | August 17, 2008, 8:15 am |
| Fork, processes and exit codes? | February 3, 2005, 2:36 am |
| Text descriptions of signal codes? | July 27, 2006, 12:11 pm |
| How to get the return codes for shell commands | January 19, 2007, 7:24 am |
| translating MS Word codes using regexps | April 27, 2007, 8:05 am |
| Dynamically Generating A Format | February 8, 2005, 7:33 pm |
| Online forum source codes in Perl/CGI without SQL? | October 23, 2004, 12:00 am |
| Making Net::SMTP Robust: Return Codes | May 27, 2005, 12:16 pm |
|