Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

defined array question

by toadi (Chaplain)
on Aug 30, 2002 at 10:49 UTC ( [id://194060]=perlquestion: print w/replies, xml ) Need Help??

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

Hello, I have a program written bij someone else. But I can't figure one thing out :)

I print out the array so I'm sure the array has elements.
I printout $#user_geg and it prints 27.
I printout $user_geg[27] and prints it's value

Then the other guy written:
if( defined @user_geg[0..$#user_geg] ) { }

On a SunOS infratest 5.8 Generic_108528-03 sun4u sparc SUNW,Ultra-80 with This is perl, v5.6.1 built for sun4-solaris

This works...

On a SunOS fritz 5.8 Generic sun4u sparc SUNW,Ultra-1 with This is perl, v5.6.0 built for sun4-solaris
I had to write:
if( defined @user_geg[0..$#user_geg-1] ) { }

To get in the if????
Plz enlighten me!!!!!!

--
My opinions may have changed,
but not the fact that I am right

Edit: Added <code> tags. larsen

Replies are listed 'Best First'.
Re: defined array question
by davorg (Chancellor) on Aug 30, 2002 at 13:16 UTC

    perldoc -f defined tells us:

    Use of "defined" on aggregates (hashes and arrays) is deprecated. It used to report whether memory for that aggregate has ever been allocated. This behavior may disappear in future versions of Perl.

    So that's a bad thing and you shouldn't do that.

    But you're not using defined on an array, you're using defined on an array slice. perldoc -f defined is silent on what this does, so the behaviour is undefined. Which means that it's quite possible for it to change between versions of Perl or different architectures.

    As others have pointed out, it's difficult to really know what you are trying to do there. Once you've worked it out, we can help you to reimplement it in "real" Perl.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      If you read my root post you'll see it was written by a other programmer and also I'm trying to figure out what it does or what he wanted to accomplish! Off course the easiest way is to ask him, but it was a consultant who already left the company an even left the country :)

      I'm just trying to fix some code and add some functionality in 'his' code...



      --
      My opinions may have changed,
      but not the fact that I am right

      But you're not using defined on an array, you're using defined on an array slice. perldoc -f defined is silent on what this does

      Considering that an array slice still returns an array I'd say the behaviour would be the same and should be considered depreciated. The only exception I see is if the slice is just one element, then you are performing defined on a scalar which I assume is still okay. :)

      Chris

      Lobster Aliens Are attacking the world!
        Considering that an array slice still returns an array I'd say the behaviour would be the same

        Nope. An array slice returns a list, not an array.

        depreciated

        That's "deprecated". "Depreciated" means something completely different.

        --
        <http://www.dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

Re: defined array question
by htoug (Deacon) on Aug 30, 2002 at 11:43 UTC
    I don't get the meaning of this: if( defined @user_geg[0..$#user_geg] ) { }

    Now lets see what it means.

    • 0..$#user_geg creates a list of integers from 1 to 27 (in this case). OK.
    • @user_geg[0..$#user_geg] return an arrayslice consisting of elements 1..27 of the array user_geg. By my book this is the same as @user_geg. Hmmm not so good...
    • defined @user_geg (leaving out the 0..27 part for now) is meaningless:
      perldoc -f defined says: Use of "defined" on aggregates (hashes and arrays) is deprecated.. Not OK!

    What you probably mean is "if there are any elements in the array 'user_egg' then ..." which can be expressed as
    if (@user_geg) { }
    See - much simpler.

    Your original question as to why you have to add the -1 is probably answered by the meaninglessness of defined @user_geg.

Re: defined array question
by BrowserUk (Patriarch) on Aug 30, 2002 at 10:57 UTC

    Your right! That doesn't make much sense. Either you have a bad installation on one machine, or there is something going on in your script that you aren't telling us.

    Like, what error did you get that caused you to change it?

    If you would post the (appropriate part of, if big) script, along with the exact error message (cut&paste rather than copy)-- don't forget to indicate which line of the code your post is the line referred to in the error message--we might be able to help you further.


    Well It's better than the Abottoire, but Yorkshire!
      I do a fetchrow from a db to fill tha array. The script generates no errors!!!! It just doesn't get into the if!!!!

      --
      My opinions may have changed,
      but not the fact that I am right

        In the absence of further info, I'd have to suspect a bad build then.

        Looking at this further, I am a bit confused as to what the if line does anyway. When I saw it, I thought it was a smart way to test all the values in @user_geg for definedness, but I just ran the following script:

        #! perl -w use strict; my @test = ( 0,1,2,3,4,5,6,7,8,9); print 'OK'.$/ if defined( @test[0 .. $#test] ); $test[5] = undef; print 'OK'.$/ if defined( @test[0 .. $#test] ); @test[0 .. $#test] = (undef, undef, undef, undef, undef, undef, undef, + undef, undef, 1); print 'OK'.$/ if defined( @test[0 .. $#test] ); $test[$#test] = undef; print 'OK'.$/ if defined( @test[0 .. $#test] ); __END__ # Output C:\test>test.pl OK OK OK C:\test>

        But as you can see, the only value being tested is the last value. Which makes sense as an array slice is just a list, and a list in a scalar context is just its last value. Which means that the test is equivalent to

        if ( $user_geg[$#user_geg] ) {...}

        That means that your change to $#user_geg-1 is testing the last but one value, which strongly suggests that the last value is undefined?

        My only suggestion is to preceed the if with print defined $user_geg[27]; and see what you get? But if you are seeing output from print $user_geg[27];, it should be defined!

        Sorry I can't suggest better.


        Well It's better than the Abottoire, but Yorkshire!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://194060]
Approved by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-16 21:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found