blackadder has asked for the wisdom of the Perl Monks concerning the following question:

what is the @$ construct means in Perl please?

I got this bit of code which works fine but i need to reference items in the @$OrigArray individually.
my $OrigArray = $worksheet->Range("A76:B9107")->{'Value'}; for (@$OrigArray) { print ++$count, ":", join(",", @$_), "\n"; }
Thanks

Replies are listed 'Best First'.
Re: The @$ array construct
by broquaint (Abbot) on Jun 26, 2003 at 11:46 UTC
    That is dereferencing the array reference in $_ e.g
    my @aoa = ( ['a' .. 'c'], ['d' .. 'f'], ['g' .. 'i'] ); print "[", @$_, "]\n" for @aoa; __output__ [abc] [def] [ghi]
    That iterates through @aoa aliasing each element to $_ and then the array reference in $_ is being dereferenced in the print statement. So to access individual elements we just dereference them like so
    my @aoa = ( ['a' .. 'c'], ['d' .. 'f'], ['g' .. 'i'] ); print "first element - ", $_->[0], "\n" for @aoa; __output__ first element - a first element - d first element - g
    See. tye's References quick reference and perlreftut for a good starter on references and dereferencing.
    HTH

    _________
    broquaint

Re: The @$ array construct
by dragonchild (Archbishop) on Jun 26, 2003 at 11:47 UTC
    $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.

      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.

Re: The @$ array construct
by diotalevi (Canon) on Jun 26, 2003 at 11:49 UTC

    $OrigArray is a plain scalar variable. It probably contains an array reference. Saying @$OrigArray is equivalent to @{$OrigArray} and is somewhat like saying @array (if you had a different array named @array). In this case the element access syntax differs slighly because you are working with an array reference and not directly. For example, $array[0] would be written as $OrigArray->[0]. In fact, you should immediately run over to perlreftut.

Re: The @$ array construct
by derby (Abbot) on Jun 26, 2003 at 11:49 UTC
    See the "Using references" section of perlref. Basically $OrigArray is a reference to an array. From perlref:

    2. Anywhere you'd put an identifier (or chain of identi­fiers) as part of a variable or subroutine name, you can replace the identifier with a BLOCK returning a reference of the correct type.

    So to dereference $OrigArray, you would use @{$OrigArray}; however, perl allows you to omit the curlies for simple cases so most people write that as @$OrigArray.

    -derby

Re: The @$ array construct
by Zaxo (Archbishop) on Jun 26, 2003 at 11:59 UTC

    Dereferencing of this kind can be clarified to the eye with the notation @{$OrigArray}. Similarly, you can write %{$hashref}, ${$scalarref}, or even ${scalar} and @{array} which are useful in quoted concatenations.

    After Compline,
    Zaxo