Simple array iteration not working?

Discussion in 'Perl Misc' started by brian.haines@gmail.com, Jun 6, 2009.

  1. Guest

    This code:

    my @sref = @{$softwareListRef};
    print Dumper(@sref);

    for (my $i = 0; $i<@sref ; $i++) {
    my $sn = $sref[$i];
    print Dumper($sn);

    Generates this output:

    $VAR1 = [
    'autoTestSoftware001',
    'autoTestSoftware050'
    ];
    $VAR1 = [
    'autoTestSoftware001',
    'autoTestSoftware050'
    ];

    Note that the array @sref is properly de-referenced and printed. Then,
    inexplicably, the array item is selected by index and when printed, is
    equal to the original array. Foreach does the same thing. What's going
    on?
     
    , Jun 6, 2009
    #1
    1. Advertising

  2. wrote:
    > This code:
    >
    > my @sref = @{$softwareListRef};
    > print Dumper(@sref);
    >
    > for (my $i = 0; $i<@sref ; $i++) {
    > my $sn = $sref[$i];
    > print Dumper($sn);
    >
    > Generates this output:
    >
    > $VAR1 = [
    > 'autoTestSoftware001',
    > 'autoTestSoftware050'
    > ];
    > $VAR1 = [
    > 'autoTestSoftware001',
    > 'autoTestSoftware050'
    > ];
    >
    > Note that the array @sref is properly de-referenced


    @sref is not dereferenced in your example. And you can't dereferenced
    an array, only a scalar. In your example $softwareListRef is dereferenced.


    > and printed. Then,
    > inexplicably, the array item is selected by index and when printed, is
    > equal to the original array. Foreach does the same thing. What's going
    > on?




    John
    --
    Those people who think they know everything are a great
    annoyance to those of us who do. -- Isaac Asimov
     
    John W. Krahn, Jun 6, 2009
    #2
    1. Advertising

  3. Guest

    Thanks for pointing out the incorrect problem description. You're
    right $softwareListRef is de-referenced, not @sref.

    Any ideas why this would be happening? A similar block of code in the
    next function in the same file works.
     
    , Jun 6, 2009
    #3
  4. kj Guest

    In <> writes:

    >This code:


    > my @sref = @{$softwareListRef};
    > print Dumper(@sref);


    > for (my $i = 0; $i<@sref ; $i++) {
    > my $sn = $sref[$i];
    > print Dumper($sn);


    >Generates this output:


    >$VAR1 = [
    > 'autoTestSoftware001',
    > 'autoTestSoftware050'
    > ];
    >$VAR1 = [
    > 'autoTestSoftware001',
    > 'autoTestSoftware050'
    > ];


    >Note that the array @sref is properly de-referenced and printed. Then,
    >inexplicably, the array item is selected by index and when printed, is
    >equal to the original array. Foreach does the same thing. What's going
    >on?



    I'm not sure what you expect. In your code, @sref is an array
    containing *exactly one element* that happens to be an arrayref,
    pointing to an array of two elements. As far as Dumper is concerned,
    Dumper(@sref) is no different from Dumper($sref[0]).

    Put this right before the code you showed:

    $softwareListRef = $softwareListRef->[0];

    and see if the resulting code behaves more along the lines of what
    you want. If so, this means that the way you are constructing
    $softwareListRef is incorrect (you have one extra level of
    referencing).

    kj



    --
     
    kj, Jun 7, 2009
    #4
  5. Guest

    On Sat, 6 Jun 2009 12:00:54 -0700 (PDT), wrote:

    >This code:
    >
    > my @sref = @{$softwareListRef};
    > print Dumper(@sref);
    >
    > for (my $i = 0; $i<@sref ; $i++) {
    > my $sn = $sref[$i];
    > print Dumper($sn);
    >
    >Generates this output:
    >
    >$VAR1 = [
    > 'autoTestSoftware001',
    > 'autoTestSoftware050'
    > ];
    >$VAR1 = [
    > 'autoTestSoftware001',
    > 'autoTestSoftware050'
    > ];
    >
    >Note that the array @sref is properly de-referenced and printed. Then,
    >inexplicably, the array item is selected by index and when printed, is
    >equal to the original array. Foreach does the same thing. What's going
    >on?


    This is right, @sref contains a single ref to
    [
    'autoTestSoftware001',
    'autoTestSoftware050'
    ]
    then dumped, then dumped again in the for loop, thats why VAR1,VAR1..
    Below is using a ref to a double element array of ref's:

    use strict;
    use warnings;
    use Data::Dumper;

    my @ar1 = ('autoTestSoftware001','autoTestSoftware050');
    my @ar2 = ('autoTestSoftware051','autoTestSoftware100');
    my $softwareListRef = [\(@ar1, @ar2)];

    # same as:
    # my $softwareListRef = [
    # ['autoTestSoftware001','autoTestSoftware050'],
    # ['autoTestSoftware051','autoTestSoftware100'],
    # ];

    my @sref = @{$softwareListRef};
    print Dumper(@sref);

    for my $sn (@sref) {
    print Dumper($sn);
    }

    __END__

    $VAR1 = [
    'autoTestSoftware001',
    'autoTestSoftware050'
    ];
    $VAR2 = [
    'autoTestSoftware051',
    'autoTestSoftware100'
    ];
    $VAR1 = [
    'autoTestSoftware001',
    'autoTestSoftware050'
    ];
    $VAR1 = [
    'autoTestSoftware051',
    'autoTestSoftware100'
    ];

    -sln
     
    , Jun 7, 2009
    #5
  6. Guest

    On Sun, 07 Jun 2009 11:47:35 -0700, wrote:

    >On Sat, 6 Jun 2009 12:00:54 -0700 (PDT), wrote:
    >
    >>This code:
    >>
    >> my @sref = @{$softwareListRef};
    >> print Dumper(@sref);
    >>
    >> for (my $i = 0; $i<@sref ; $i++) {
    >> my $sn = $sref[$i];
    >> print Dumper($sn);
    >>
    >>Generates this output:
    >>
    >>$VAR1 = [
    >> 'autoTestSoftware001',
    >> 'autoTestSoftware050'
    >> ];
    >>$VAR1 = [
    >> 'autoTestSoftware001',
    >> 'autoTestSoftware050'
    >> ];
    >>
    >>Note that the array @sref is properly de-referenced and printed. Then,
    >>inexplicably, the array item is selected by index and when printed, is
    >>equal to the original array. Foreach does the same thing. What's going
    >>on?

    >
    >This is right, @sref contains a single ref to
    > [
    > 'autoTestSoftware001',
    > 'autoTestSoftware050'
    > ]


    The only way to get this output is if

    $softwareListRef = [
    [
    'autoTestSoftware001',
    'autoTestSoftware050'
    ]
    ];

    Where Dumper @sref prints the single 'element' in the array,
    which is the ref ['autoTestSoftware001','autoTestSoftware050']

    Then Dumper $sref[0] which is also the same 'element' which
    is the same ref.

    You have done basically the same operation.

    If you had coerrced the element as an array @{$sref[0]},
    its contents would have been printed without the [] and as

    $VAR1 = 'autoTestSoftware001';
    $VAR2 = 'autoTestSoftware050';


    -sln
     
    , Jun 8, 2009
    #6
  7. Guest

    On Sun, 07 Jun 2009 21:00:50 -0700, wrote:

    >On Sun, 07 Jun 2009 11:47:35 -0700, wrote:
    >
    >>On Sat, 6 Jun 2009 12:00:54 -0700 (PDT), wrote:
    >>
    >>>This code:
    >>>
    >>> my @sref = @{$softwareListRef};
    >>> print Dumper(@sref);
    >>>
    >>> for (my $i = 0; $i<@sref ; $i++) {
    >>> my $sn = $sref[$i];
    >>> print Dumper($sn);
    >>>
    >>>Generates this output:
    >>>
    >>>$VAR1 = [
    >>> 'autoTestSoftware001',
    >>> 'autoTestSoftware050'
    >>> ];
    >>>$VAR1 = [
    >>> 'autoTestSoftware001',
    >>> 'autoTestSoftware050'
    >>> ];
    >>>
    >>>Note that the array @sref is properly de-referenced and printed. Then,
    >>>inexplicably, the array item is selected by index and when printed, is
    >>>equal to the original array. Foreach does the same thing. What's going
    >>>on?

    >>
    >>This is right, @sref contains a single ref to
    >> [
    >> 'autoTestSoftware001',
    >> 'autoTestSoftware050'
    >> ]

    >
    >The only way to get this output is if
    >
    >$softwareListRef = [
    > [
    > 'autoTestSoftware001',
    > 'autoTestSoftware050'
    > ]
    >];
    >
    >Where Dumper @sref prints the single 'element' in the array,
    >which is the ref ['autoTestSoftware001','autoTestSoftware050']
    >
    >Then Dumper $sref[0] which is also the same 'element' which
    >is the same ref.
    >
    >You have done basically the same operation.
    >
    >If you had coerrced the element as an array @{$sref[0]},
    >its contents would have been printed without the [] and as
    >
    >$VAR1 = 'autoTestSoftware001';
    >$VAR2 = 'autoTestSoftware050';
    >
    >


    Sorry about that, one more thing. If you pass an @array
    to Dumper it will print its contents. If you pass a $scalar
    to Dumper it will print its contents. Otherwise,
    there is no way to tell what it is.

    So if an array reference, it will be shown with brackets with its contents expanded.
    Identical if its an array with a single element array reference, which is what you
    found in your case.
    All content are recursively expanded.

    -sln
     
    , Jun 8, 2009
    #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. Radith
    Replies:
    12
    Views:
    540
    Chris Smith
    Jan 13, 2005
  2. Bryan
    Replies:
    14
    Views:
    483
    Cameron Laird
    Aug 15, 2007
  3. Rudi
    Replies:
    5
    Views:
    5,306
  4. Esmail
    Replies:
    4
    Views:
    227
    Esmail
    Apr 22, 2009
  5. Blake Miller

    Simple iteration in a function problem

    Blake Miller, Dec 1, 2006, in forum: Ruby
    Replies:
    24
    Views:
    239
    David Vallner
    Dec 2, 2006
Loading...

Share This Page