473,378 Members | 1,370 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

[perl-python] 20050116 defining a function

© # the following is a example of defining
© # a function in Python.
©
© def fib(n):
© """This prints n terms of a sequence
© where each term is the sum of previous two,
© starting with terms 1 and 1."""
© result=[];a=1;b=1
© for i in range(n):
© result.append(b)
© a,b=b,a+b;
© result.insert(0,1)
© del result[-1]
© return result
©
© print fib(6)
©
© # note the use of .insert to insert 1 at
© # the beginning of a list, and Òdel
© # result[-1]Ó to remove the last element
© # in a list. Also, the string
© # immediately following the function
© # definition is the function's
© # documentation.
©
© # the unusual syntax of a.insert() is
© # what's known as Object Oriented syntax style.
©
© # try writing a factorial function.
©
© -------------------
© # the equivalent perl version is this:
©
© =pod
©
© fib(n) prints n terms of a sequence where each
© term is the sum of previous two,
© starting with terms 1 and 1.
©
© =cut
©
© use strict;
© my @result;
© my ($a, $b);
©
© sub fib($) {
© my $n= @_[0];
© @result=();$a=1;$b=1;
© for my $i (1..$n){
© push @result, $b;
© ($a,$b)=($b,$a+$b);
© }
© unshift @result, 1;
© pop @result;
© return @result;
© }
©
© use Data::Dumper;
© print Dumper [fib(5)];
©
© # the =pod and =cut
© # is perl's way of demarking inline
© # documentation called POD.
© # see Òperldoc -t perlpodÓ
© # note: the empty line around it
© # is necessary, at least in perl version
© # 5.6 up to ~2002.
©
© # the Òuse strict;Ó is to make perl's
© # loose syntax stricter by enforcement.
© # Its use is encouraged by
© # perl gurus, but not all standard
© # packages use it.
©
© # the ÒmyÓ are used to declare variables.
© # necessary under Òuse strict;Ó
© # see Òperldoc -t strictÓ
©
© # the $ in fib($) is there to declare
© # that fib has a parameter of one scalar.
© # Its use is however optional and uncommon.
© # it is used for clarity but
© # has also met with controversy by
© # perl gurus as being unperl.
© # see Òperldoc perlsubÓ for ref.
©
© # the @_[0] is the first element of the
© # array @_. The @_ array is a predefined
© # array, holding arguments to subroutines.
© # see Òperldoc -t perlvarÒ
©
© # see
© # perldoc -tf unshift
© # perldoc -tf pop
©
© # the last line, [fib(5)], is basically
© # to make it a memory address of a copy of
© # the list returned by fib(5), so that
© # Dumper can print it.
© # see Òperldoc -t perldataÓ or perlref
© # for unix-styled technicalities.
©
© Xah
© xa*@xahlee.org
© http://xahlee.org/PageTwo_dir/more.html

Jul 18 '05 #1
5 1280
Xah Lee wrote:
© my $n= @_[0];


Do you ever test your code before making fun of yourself in front of
millions?

*plonk*

--Ala

Jul 18 '05 #2
Ala Qumsieh wrote:
© my $n= @_[0];


Do you ever test your code before making fun of yourself in front of millions?


this perl usability study is getting more and more interesting. who
needs television?

</F>

Jul 18 '05 #3
errata:

* the variables in the perl section should be declared inside the
subroutine.
* the @_[0] should've been $_[0]

thanks for Dave Cross for pointing them out.

* the Mathematica Apply should be Select...
Xah
xa*@xahlee.org
http://xahlee.org/PageTwo_dir/more.html

Jul 18 '05 #4
Xah Lee wrote:
errata:

* the variables in the perl section should be declared inside the
subroutine.
* the @_[0] should've been $_[0]

thanks for Dave Cross for pointing them out.

* the Mathematica Apply should be Select...
Xah
xa*@xahlee.org
http://xahlee.org/PageTwo_dir/more.html


Here's a thought: don't post code you haven't tested!
--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
Jul 18 '05 #5
Ala Qumsieh <no******@email.com> wrote:
Xah Lee wrote: *plonk*

Man, you are way behind the curve.

I did that over 3 years ago! [1]


[1] Message-ID: <7f**************************@posting.google.com >

I followed the link in that post on a hunch, and found
he says himself that he is a troll. Go figure...

--
Tad McClellan SGML consulting
ta***@augustmail.com Perl programming
Fort Worth, Texas
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mark Wilson CPU | last post by:
This must be easy, but I'm missing something... I want to execute a Perl script, and capture ALL its output into a PHP variable. Here are my 2 files: -------------------------------------...
6
by: John Smith | last post by:
Hello, I have a rather odd question. My company is an all java/oracle shop. We do everything is Java... no matter what it is... parsing of text files, messaging, gui you name it. My question...
3
by: David F. Skoll | last post by:
Hi, I'm tearing my hair out on this one. I'm trying to embed a Perl interpreter into a C program. I need to be able to create and destroy the interpreter periodically, but will never actually...
1
by: Julia Bell | last post by:
I would like to run the same script on two different platforms. The directory in which the script(s) will be stored is common to the two platforms. (I see the same directory contents regardless...
1
by: smsabu2002 | last post by:
Hi, I am facing the build problem while installing the DBD-MySql perl module (ver 2.9008) using both GCC and CC compilers in HP-UX machine. For the Build using GCC, the compiler error is...
13
by: Otto J. Makela | last post by:
I'm trying to install to php the Perl-1.0.0.tgz package (from http://pecl.php.net/package/perl, enabling one to call perl libraries) to a pre-existing Solaris system. Unfortunately, the attempt...
6
by: surfivor | last post by:
I may be involved in a data migration project involving databases and creating XML feeds. Our site is PHP based, so I imagine the team might suggest PHP, but I had a look at the PHP documentation...
223
by: Pilcrow | last post by:
Given that UNIX, including networking, is almost entirely coded in C, how come so many things are almost impossible in ordinary C? Examples: Network and internet access, access to UNIX...
4
by: vijayarl | last post by:
Hi All, i have the following software installed in my system : 1.OS: Win2k 2.Eclipse Version used :3.4.0 & even the perl too... 1. I have imported the my own perl project in Eclipse, when i...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.