insert codes dynamically

Discussion in 'Perl Misc' started by Rose, Mar 6, 2008.

  1. Rose

    Rose Guest

    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);
    Rose, Mar 6, 2008
    #1
    1. Advertising

  2. Rose

    Rose Guest

    "Frank Seitz" <> wrote in message
    news:...
    > 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.
    Rose, Mar 6, 2008
    #2
    1. Advertising

  3. Rose

    Rose Guest

    "Frank Seitz" <> wrote in message
    news:...
    > 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,
    );
    Rose, Mar 6, 2008
    #3
  4. Rose

    Rose Guest

    "Frank Seitz" <> wrote in message
    news:...
    > Rose wrote:
    >>
    >> 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,
    >> );

    >
    > Can't tell. What expects add_track() as second parameter?
    > (first parameter is the ref to the panel-Object)
    >
    > 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


    It can accept a number of objects $obj1,$obj2, ..., $objn in square
    brackets. the problem is that "n" is unknown beforehand, and therefore can't
    be hard-coded. I know that in Matlab a function called repmat may help...
    Rose, Mar 6, 2008
    #4
  5. Rose

    Ben Morrow Guest

    [please don't quote signatures]

    Quoth "Rose" <>:
    > "Frank Seitz" <> wrote in message
    > news:...
    > > Rose wrote:
    > >>
    > >> 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,
    > >> );

    > >
    > > Can't tell. What expects add_track() as second parameter?
    > > (first parameter is the ref to the panel-Object)

    >
    > It can accept a number of objects $obj1,$obj2, ..., $objn in square
    > brackets. the problem is that "n" is unknown beforehand, and therefore can't
    > be hard-coded. I know that in Matlab a function called repmat may help...


    If you have an array

    @obj = (1, 2, 3, 4);

    then the expression

    [1, 2, 3, 4]

    is exactly equivalent to

    [@obj]

    .. This is generally true: whenever you have a series of comma-separated
    items in list context, you can insert an array into the list and it will
    be interpolated. The exception is the argument lists of functions like
    'push' which have prototypes and so treat literal arrays specially.

    Under many circumstances, it would be better to use

    \@obj

    as this doesn't make a copy of the array. You can do this if you know
    the function you are calling doesn't modify the passed-in array (or if
    you don't care if it trashes @obj).

    Note that @obj is *not* equivalent to

    $obj1, $obj2, $obj3, ...

    if you were thinking that; $obj[1] is not the same as $obj1. If those
    variables weren't just an example, they should have been in an array to
    start with.

    I would suggest you read perldoc perldsc and perldoc perlreftut.

    Ben
    Ben Morrow, Mar 6, 2008
    #5
  6. Rose

    ccc31807 Guest

    On Mar 6, 12:16 am, "Rose" <> wrote:
    > "Frank Seitz" <> wrote in message
    >
    > news:...
    >
    >
    >
    > > 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.


    In the first script, write the array to a text file, values space
    separated with each list on a new line, like this:
    20 30 A
    40 50 B
    60 70 C
    ... etc

    In the second script, read in the file line by line and recreate the
    data structure possibly as an array composed of array references.

    I assume that you can read and write to a text file in your directory,
    and that you will overwrite the same file each day so you can use
    static file names.

    CC
    ccc31807, Mar 6, 2008
    #6
  7. Rose

    Ben Morrow Guest

    Quoth "Rose" <>:
    >
    > 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?


    Do they need to be separate scripts? Are they run at different times? If
    you are using separate scripts simply as a way of putting the code in
    separate files, you may want to use modules instead.

    The easy and straightforward way to pass data from Perl to Perl is the
    use the Storable module, which is core as of 5.8. In the first script
    you say

    use Storable qw/store/;

    store \@arr, 'file' or die "store failed";

    and then in the second

    use Storable qw/retrieve/;

    my $aref = retrieve 'file' or die "retrieve failed";

    If you want the data to be human-readable, or readable from another
    language, you could use YAML instead.

    Ben
    Ben Morrow, Mar 6, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Greg  --
    Replies:
    4
    Views:
    2,138
  2. cannontrodder
    Replies:
    1
    Views:
    711
    cannontrodder
    Jul 25, 2006
  3. Replies:
    2
    Views:
    2,798
    Malcolm
    Aug 20, 2005
  4. Mr. Ken
    Replies:
    1
    Views:
    286
    Kai-Uwe Bux
    Jun 2, 2006
  5. Allen
    Replies:
    1
    Views:
    628
    Mark Rae [MVP]
    Dec 3, 2007
Loading...

Share This Page