Dynamic Ref names

Discussion in 'Perl Misc' started by Warrick FitzGerald, Feb 16, 2004.

  1. Hi All,

    I'm trying to dynamically create worksheets using Spreadsheet::WriteExcel.

    I'm looking through a log file and would like to create a worksheet for
    each date.

    In all example I can find you simply do:
    $mySheet = $workbook->addworksheet("somename");

    then to add data to that you simply do:
    $mySheet->write(....);

    That works great until I try to dynamically create $mySheet.

    --------------
    This is what I tried.

    $date = $Fields[0]; <--- Get the date from the Logfile

    ${$date} = $workbook->addworksheet($date); <-- This created the workbook
    for each date. date is of course formatted to be excel friendly name.

    My problem come when trying to call the write method on my dynamic name.

    ${$date}->write(...) does not work ?
    %{$date}->write(...) does not work ?

    I have tried everything I can think of. Can someone please tel me how I
    can call the write method on a dynamically named worksheet?

    Thanks
    Warrick FitzGerald
     
    Warrick FitzGerald, Feb 16, 2004
    #1
    1. Advertising

  2. Warrick FitzGerald

    Ben Morrow Guest

    Warrick FitzGerald <> wrote:
    >
    > I'm trying to dynamically create worksheets using Spreadsheet::WriteExcel.
    >
    > I'm looking through a log file and would like to create a worksheet for
    > each date.
    >
    > In all example I can find you simply do:
    > $mySheet = $workbook->addworksheet("somename");
    >
    > then to add data to that you simply do:
    > $mySheet->write(....);
    >
    > That works great until I try to dynamically create $mySheet.
    >
    > --------------
    > This is what I tried.
    >
    > $date = $Fields[0]; <--- Get the date from the Logfile
    >
    > ${$date} = $workbook->addworksheet($date); <-- This created the workbook
    > for each date. date is of course formatted to be excel friendly name.


    Are you using strictures? Why not?

    You don't need to name the variable after the sheet name:

    $mySheet = $workbook->addworksheet($date);

    will work perfectly well. If you're trying to keep hold of several
    sheets at once, one for each date, you should use a hash, not the symbol
    table:

    my %sheets;

    $sheets{$date} = $workbook->addworksheet($date);

    Ben

    --
    $.=1;*g=sub{print@_};sub r($$\$){my($w,$x,$y)=@_;for(keys%$x){/main/&&next;*p=$
    $x{$_};/(\w)::$/&&(r($w.$1,$x.$_,$y),next);$y eq\$p&&&g("$w$_")}};sub t{for(@_)
    {$f&&($_||&g(" "));$f=1;r"","::",$_;$_&&&g(chr(0012))}};t #
    $J::u::s::t, $a::n::eek:::t::h::e::r, $P::e::r::l, $h::a::c::k::e::r, $.
     
    Ben Morrow, Feb 16, 2004
    #2
    1. Advertising

  3. Warrick FitzGerald wrote:

    > This is what I tried.
    >
    > $date = $Fields[0]; <--- Get the date from the Logfile
    >
    > ${$date} = $workbook->addworksheet($date); <-- This created the workbook
    > for each date. date is of course formatted to be excel friendly name.
    >
    > My problem come when trying to call the write method on my dynamic name.


    Here is a short example:

    #!/usr/bin/perl -w

    use strict;
    use Spreadsheet::WriteExcel;

    my $workbook = Spreadsheet::WriteExcel->new("dates.xls");

    my %worksheets;

    my $date = '2004-02-18';


    $worksheets{$date} = $workbook->addworksheet($date);

    $worksheets{$date}->write("A1", "Hello world");

    __END__


    John.
    --
    # Count the number of lines in a file.
    perl -le 'print $==()=<>' file
     
    John McNamara, Feb 18, 2004
    #3
    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. Replies:
    0
    Views:
    363
  2. Replies:
    22
    Views:
    794
    peter koch
    Apr 30, 2008
  3. Replies:
    6
    Views:
    378
    James Kanze
    Apr 29, 2008
  4. Navindra Umanee

    strong ref from weak ref?

    Navindra Umanee, Feb 12, 2005, in forum: Ruby
    Replies:
    2
    Views:
    158
    Navindra Umanee
    Feb 12, 2005
  5. Juha Nieminen
    Replies:
    13
    Views:
    651
    Edek Pienkowski
    Aug 29, 2012
Loading...

Share This Page