in reply to The @$ array construct

$OrigArray is a reference to an array. @$OrigArray is the array referenced by the reference $OrigArray.
# Grab an array reference from Spreadsheet::ParseExcel my $OrigArray = $worksheet->Range("A76:B9107")->{'Value'}; # Iterate through that array reference for (@$OrigArray) { # At this point, the elements in $OrigArray are now each # put into $_. However, $_ seems to, itself, be an array # reference. (I've never used this feature of Spreadsheet::ParseEx +cel, # but I know the syntax explanation is correct, based on # the code in front of me. print ++$count, ":", join(",", @$_), "\n"; }
Sometimes, it can help to explain what it is you're trying to do. Maybe we have solutions to your root problem, not your symptom.

------
We are the carpenters and bricklayers of the Information Age.

Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Replies are listed 'Best First'.
Re: Re: The @$ array construct
by blackadder (Hermit) on Jun 27, 2003 at 16:12 UTC
    Basically I have inherited a massive excel workbook. In one of the worksheets I have a column where I have server names and the next column I have user Ids like this;


    server1 JohnB
    server5 JohnB
    server3 LisaG
    Server2 LisaG
    server7 AdamX
    server1 Adamx
    server3 PaulK
    server5 PaulK


    For each account name there is only one server where the users home drives are located, what I need to do is to:
    1- Check which is the correct server.
    2- Edit the excel sheet by inserting ok next to the correct server name or Del for the incorrect one.
    So i get this results below;

    server1 JohnB Ok
    server5 JohnB Del
    server3 LisaG Del
    Server2 LisaG Ok
    server7 AdamX Del
    server1 Adamx Ok
    server3 PaulK Ok
    server5 PaulK Del


    I have written a script that that checks which is the correct home server using combination of AdminMisc and Netadmin, but I need to do is get the script to edit this massive list with correct information rather than me doing it manually so I wrote this bit (or nicked it form someone here)
    use strict 'vars'; use OLE; use Win32::NetAdmin; use Win32::AdminMisc; my @Domains = ('Dom_UK','Dom_US'); my $xl = CreateObject OLE 'Excel.Application' || die $!; my $workbook = $xl->Workbooks->Open("C:\\myfiles\\test.xls"); my $worksheet = $workbook->Worksheets(1); my $xls = $worksheet->Range("A1:B10000")->{'Value'}; my @DataLst; my $count=0; for (@$xls) { print ++$count." @$_\n"; }
    Now, I need to have a hash where I have the following information
    Row -> Row number, ID -> User ID, Server -> the server where the correct home drive is.
    And push each record (or Hash) into and a one big array, so that I can loop this array, and access each record, check the user ID against the server is correct or not, if its correct then I use the row number (columns is constant and = 3) to insert ok or del. And that’s it. But I am not sure on how to split $@_ into two element $srv and $id, insert them into and array and loop through the array to access each item the amend the excel file with the findings. Any suggestions or Perls of wisdom (with and example or 2) are welcome

    Many thanks indeed.
      Try this on for size:
      1. Build a hash that maps userid to servername.
      2. Iterate through the rows in your excel workbook. This will give you an array reference, each element of which is an array reference.
      3. Grab the userid element (let's say it's column F) and server (in G).
      4. Do a compare and you're done!
      my %User_Server = MapUsersToServer(); my $Rows = $worksheet->SomeMethodICan'tRemember(); foreach my $row (@$Rows) { my ($user_id, $server) = ($row->[5], $row->[6]); if ($User_Server{$user_id} ne $server) { # Do something useful here. } }
      I hope that helps.

      ------
      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

        ($row->5, $row->6); did it for me,....Many thanks
        cheers.