Everything2
Near Matches
Ignore Exact
Full Text
Everything2

wantarray

created by eric+

(idea) by eric+ (5.6 y) (print)   ?   (I like it!) 1 C! Sat Jun 03 2000 at 8:03:22

wantarray is yet another reason why perl is a great programming language. You can use wantarray to have change what your function returns based upon the context in which it was called.

For example, say you have a function called unique() which takes an @array as its argument. You can use wantarray to have unique() return a list of unique elements from @array or the number of unique elements depending upon context.

sub unique {
  my @array = @_;

  my %temp = map { $_ => 1 } @array;

  if ( wantarray() ) {
    return sort( keys( %temp ) );         # the unique elements
  }
  else {
    return scalar( keys( %temp ) );    # the number of unique elements
  }
}

You can now call unique() as follows:

  my @unique_array = unique( @array );      # get a sorted unique list

  my $unique_number = unique( @array );    # get the number of elements.


printable version
chaos

unique array element context
Perl argument programming language List
call function
Y'know, if you log in, you can write something here, or contact authors directly on the site. Create a New User if you don't already have an account.
  Epicenter
Login
Password

password reminder
register

Everything2 Help

Cool Staff Picks
Little presents from the Node Fairy:
Islam rejects violence as a method to gain power
Concorde
Morton Feldman
Northern Ireland
Ultima Thule
Peking duck
Song Of Myself
Searle's Chinese room
The seizure I had last night
Hittite
Watching Karen laugh
Dead people are not sleeping. They are dead.
How to use a manual transmission
New Writeups
Ysardo
a brother to a sister(personal)
antigravpussy
your warm whispers(personal)
Clarke
Multiculturalism(idea)
aneurin
Earl of Landaff(person)
Heitah
Pseudocide(idea)
XWiz
Google Knol(lede)
Mythi
July 24, 2008(personal)
locke baron
The fall of Earth(fiction)
BookReader
Fear the Cold(dream)
Pavlovna
Kathleen MacInnes(person)
stainedglass
1(fiction)
kalen
Three "T"s(idea)
octillion369
Undead(idea)
archiewood
Ico(fiction)
Heisenberg
Why I love Everything2(log)
This page courtesy of The Everything Development Company