> Greets, all. This is my first post here... I'm a seasoned > C/C++ programmer, and recently started learning perl... it's > going along well, and as an exercise, I've completely Perl'ed > my webpage. I'm still a little dizzy because of the different
Howdy, cool, you'll love perl a lot there's so much you can do! > directions it's pulling me towards, but it's fun anyway. > Chaos always is :-) > > Anyway... the question: > I need to know if there is a library function in perl to > remove an element in an arbitrary position in an array. > > I have: > > @a = ( 1, 2, 3 ); > > I want it to become: > > @a = ( 1, 3 ); > > Is there any shortcut for this in perl? > > #ifdef INQUISITIVE_CHAR > > I'm keeping an array of tips (read from a file) in memory. At > strategic (at least I think so) locations in the file, a > random tip is displayed. Once it is, it has to be marked as > used. Currently, I'm doing this: > > # Tip is done; remove it > $tips[$tip_num] = $tips[$num_tips - 1]; > undef $tips[$num_tips - 1]; > --$num_tips; > > Am I doing it cleanly? > Should I use $#tips instead of a separate count variable (as > I've done), and will decrementing $#tips actually reduce the > size of the array in memory? Well you could actually put them in a hash with a number as the key and the tip as th evalue then undefine the value of a key once it's used. my %tips; $tips{'1'} = "Use perl"; $tips{'joemama'} = "make love not war"; $tips{'2'} = "Socks first then shoes"; $tips{'monkey'} = "do not stare at sun"; my $random_key = ( keys %tips )[ rand keys %tips ]; print "Tip of the second: $tips{$random_key}"; delete $tips{$random_key}; # or undef instead of delete my $next_random_key = ( keys %tips )[ rand keys %tips ]; You could even pass a hash referecn to a subroutine, have it grab the randon key, get the value, remove it form the has, and return the value. But I'll leave that for your fun or I may do it if I get time. HTH DMuey > > #endif // INQUISITIVE_CHAR ________________________________________ > > Abhijit Shylanath > http://mudeth.tripod.com/ > > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]