473,395 Members | 1,341 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,395 software developers and data experts.

Perl functions: subroutines

2
Hi all, my first time here! Can someone assist me? i need to write a "simple" perl scripts to cancatenate two strings (preferably short DNA sequences) using the subroutine function? Mine doesnt print anything.Also need general hints on subroutines.I'm still new to perl (and programming in general!).

Here is the script I had writen:
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl;
  2. #subroutine to concatenate two strings of dna
  3. #practicesubroutines1.txt
  4.  
  5. sub conc($);
  6. print "enter the dna sequence1>>";
  7. $dna1=<>;
  8. print "enter dna sequence2>>";
  9. $dna2=<>;
  10. $newdna=conc($dna);
  11. print "the new dna is $newdna\n";
  12.  
  13. sub conc($)
  14. {
  15. my $dna1=$_[0];
  16. my $dna2=$_[0];
  17. $dna="$dna1"."$dna2";
  18. return $dna;
  19. }
  20.  
Mar 8 '10 #1
4 2926
jkmyoung
2,057 Expert 2GB
Are you using <STDIN> ? eg
$dna1=<STDIN>;

Your function call has to pass both sequences in, eg:
Expand|Select|Wrap|Line Numbers
  1. $newdna=conc($dna1, $dna2);
  2. ....
  3. sub conc{
  4. $str = ""
  5. foreach (@_) { $str = $str . $1;}
  6. return $str;
  7. }
  8.  
Mar 8 '10 #2
RonB
589 Expert Mod 512MB
jkmyoung,

Where did you get that $1 var? Did you mean to use $_?
Mar 8 '10 #3
RonB
589 Expert Mod 512MB
Lets use a version that doesn't take unnecessary steps and is warnings and strict safe.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl;
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. print "enter the dna sequence1>>";
  7. chomp(my $dna1 = <>);
  8.  
  9. print "enter dna sequence2>>";
  10. chomp(my $dna2 = <>);
  11.  
  12. my $newdna = conc($dna1, $dna2);
  13. print "the new dna is $newdna\n";
  14.  
  15. sub conc {
  16.     return join('', @_);
  17. }
Mar 8 '10 #4
Little modification to your code

Expand|Select|Wrap|Line Numbers
  1. # #!/usr/bin/perl;
  2. # #subroutine to concatenate two strings of dna
  3. # #practicesubroutines1.txt
  4.  
  5.  sub conc($$);
  6.  print "enter the dna sequence1>>";
  7.  $dna1=<>;
  8. chomp($dna1);
  9.  print "enter dna sequence2>>";
  10.  $dna2=<>;
  11. chomp($dna2);
  12.  $newdna=conc($dna1,$dna2);
  13. print "the new dna is $newdna\n";
  14.  
  15.  sub conc($$)
  16.  {
  17.  my $dna1=$_[0];
  18.  my $dna2=$_[1];
  19.  $dna="$dna1"."$dna2";
  20.  return $dna;
  21.  }
Mar 26 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

77
by: Hunn E. Balsiche | last post by:
in term of its OO features, syntax consistencies, ease of use, and their development progress. I have not use python but heard about it quite often; and ruby, is it mature enough to be use for...
1
by: seema | last post by:
hi all, I am a fresher, learning the Perl programming. I have got a problem i just want to match old C style function definition, for example, void hiall() int k,j,l,m; char *dare; {
3
by: David Bear | last post by:
I have a hash function written by another organization that I need to use. It is implemented in perl. I've been attempting to decode what they are doing in their hash function and it is taking way...
2
by: * Tong * | last post by:
Hi Suppose there is a Perl equivalent function named 'ls' or 'sort', now what is a good way to pass those switches to it? I.e., I need a systematical way for Perl functions to handle dozens of...
4
by: =?ISO-8859-7?B?1/H189zt6Ocgwfrt4eve?= | last post by:
How can I convert a perl script to Python? Thank you!
7
by: jmishra | last post by:
Hi, I am executing perl script through my C++ code with the help of "perlembed" and it is working fine for normal perl systax and perl functions.But when I try to access database from the perl...
6
by: mercuryshipzz | last post by:
Hi, First I will tell my objective of this function (function one). I have a table for ex: id passwd name -- ------ ----- 1 fdhgfs werwer
6
KevinADC
by: KevinADC | last post by:
This snippet of code provides several examples of programming techniques that can be applied to most programs. using hashes to create unique results static variable recursive function...
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: 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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.