Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^2: Sorting an array of hashes

by BKB (Novice)
on Mar 27, 2008 at 08:23 UTC ( [id://676657]=note: print w/replies, xml ) Need Help??


in reply to Re: Sorting an array of hashes
in thread Sorting an array of hashes

# no need to check if exist when using foreach
#! perl use warnings; use strict; my @x = (1,2,3,4,5); delete $x[2]; for (0..4) { print $x[$_] }

Replies are listed 'Best First'.
Re^3: Sorting an array of hashes
by stiller (Friar) on Mar 27, 2008 at 08:57 UTC
    Actually, in that case you need to check if the value is defined. The element still exists, and checking for existens doesn't buy you anything.
      Running the previous version of code:
      perl arraydelete.pl Use of uninitialized value within @x in print at arraydelete.pl line 6 +. 1245
      Modified as follows using "exists":
      #! perl use warnings; use strict; my @x = (1,2,3,4,5); delete $x[2]; for (0..4) { print $x[$_] if exists ($x[$_]) }
      Run it to produce:
      perl arraydelete.pl 1245
        Update: I didn't read your post carefully enough

        You get the result you want, but the semantic is not what you indicate by using exists so I'd rather use defined in that case.

        #! perl use warnings; use strict; my @x = (1,2,3,4,5); delete $x[2]; for (0..4) { print $x[$_] }
        Run it to produce:
        Use of uninitialized value in print at checkifexists.pl line 6. 1245
        So, the element exists, but it's value is undefined.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-25 12:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found