4 comments on “Perl 1

  1. Configure ssh

    How to login on a remote host without giving a password. http://www.ece.uci.edu/~chou/ssh-key.html

    * Step 1:
    % ssh-keygen -t dsa
    Generating public/private dsa key pair.
    Enter file in which to save the key (~/.ssh/id_dsa): (just type return)
    Enter passphrase (empty for no passphrase): (just type return)
    Enter same passphrase again: (just type return)
    Your identification has been saved in ~/.ssh/id_dsa
    Your public key has been saved in ~/.ssh/id_dsa.pub
    The key fingerprint is:
    Some really long string
    %
    * Step 2:

    Then, paste the content of the local ~/.ssh/id_dsa.pub file into the file ~/.ssh/authorized_keys on the remote host.

    If you using putty use private key instead.

  2. Adding a new hard drive

    /sbin/fdisk /dev/sdb
    Then make a new partition in fdisk command line.
    /sbin/mkfs.ext3 -m 0 -j /dev/sdb1

    # mkdir /mnt/home2
    # mount /dev/sdb1 /mnt/home2
    ls /mnt/home2
    umount /mnt/home2

    Finally we add an entry to the /etc/fstab file to mount the partition when your machine boots,

    /dev/sdb1 /mnt/home2 ext3 defaults 1 1
    mount -a

    Adding a new hard drive: Simple http://linux.ncl.ac.uk/format/

    Add_a_new_hard_drive: Another method http://wiki.linuxquestions.org/wiki/Add_a_new_hard_drive

    Partitioning and Formatting Second Hard Drive – (ext3) http://www.idevelopment.info/data/Unix/Linux/LINUX_PartitioningandFormattingSecondHardDrive_ext3.shtml

    second hard drive in Ubuntu Linux http://www.smorgasbord.net/2007/06/29/how-to-install-second-hard-drive-in-ubuntu-linux/ Plotting E1

    When this method does not work or the computer does not recognize a new hard drive, it is likely that the drive is dead one.

  3. Print hash

    A easy way

    print “\%hash = @{[%hash]}\n”;
    global variable in mod.pm
    use vars qw($hideo);

    Perl Reference

    http://www.perl.com/doc/manual/html/pod/perlref.html

    $scalarref = \$foo;
    $arrayref = \@ARGV;
    $hashref = \%ENV;
    $coderef = \&handler;
    $globref = \*foo;

    @list = (\$a, \@b, \%c);
    @list = \($a, @b, %c); # same thing!

    A reference to an anonymous hash can be created using curly brackets:

    $hashref = {
    ‘Adam’ => ‘Eve’,
    ‘Clyde’ => ‘Bonnie’,
    };

    #!/usr/bin/perl
    use strict;
    use warnings;
    my $scalar = “This is a scalar”;
    my $scalar_ref = \$scalar;
    print “Reference: ” . $scalar_ref . “\n”;
    print “Dereferenced: ” . $$scalar_ref . “\n”;

    In the same way, to dereference an array reference, hash reference or a subroutine reference, you put the appropriate identifier (sigil) before the reference.

    #!/usr/bin/perl
    use strict;
    use warnings;

    my $array_ref = [‘apple’, ‘banana’, ‘orange’];
    my @array = @$array_ref;

    print “Reference: $array_ref\n”;
    print “Dereferenced: @array\n”;

    And a hash reference example:

    #!/usr/bin/perl
    use strict;
    use warnings;

    my $hash_ref = {name => ‘Becky’, age => 23};
    my %hash = %$hash_ref;

    print “Reference: $hash_ref\n”;
    print “Dereferenced:\n”;
    foreach my $k (keys %hash) {
    print “$k: $hash{$k}\n”;
    }

    The output of this program is:

    Reference: HASH(0x80f6c6c)
    Dereferenced:
    name: Becky
    age: 23

    http://www.perlmeme.org/howtos/using_perl/dereferencing.html

    =

    my %new_hash;
    my $some_key1 = 1234;
    my @some_array1 = qw/Name1 Email1 Age1/;
    $new_hash{$some_key1} = [@some_array1];

    my $some_key2 = 1235;
    my @some_array2 = qw/Name2 Email2 Age2/;
    $new_hash{$some_key2} = [@some_array2];
    $some_key3 =1236;
    my @some_array2 = qw/Name3 Email3 Age34/;
    $new_hash{$some_key3} = \@some_array2;

    for ( sort {$a $b} keys %new_hash ) {
    my @value_array = @{$new_hash{$_}};
    print “Key is $_ and Second element of array is $value_array[1],
    $value_array[0] $value_array[2]: $new_hash{$_}[0], $new_hash{$_}[1],
    $new_hash{$_}[2] \n”;
    }
    ===============
    Motif is a sub for counting motif frequency.
    #here is how it is used in a program
    Motif(\%n_best_list2,\%seqh1,\%motif,\%motif_position,\%motif_each_seq,\$motifsize);

    sub Motif
    {use strict;
    my ($namelistref,$seqh1ref,$motifref,$motif_positionref,$motif_each_seqref,$motifsize1ref)=@_;
    my $motifsize=$$motifsize1ref;
    print “##motif size $motifsize\n”;
    my (%seqAH,$size, $motif_word);
    my %hash=%$seqh1ref;

    for my $name (keys %$namelistref)
    { #print ” seq ($name) >$$seqh1ref{$name}<$hash{$name}$name\n”;
    #from 0 to the end of a sequence.
    for my $s_i (0..$size-$motifsize-1)
    { #construct motif word
    for my $m_i ($s_i..$s_i+$motifsize)
    {
    $motif_word.=$seqAH{$name}[$m_i]
    }
    #count the word frqueny
    $$motifref{$motif_word}++;
    #print “$motif_word $$motifref{$motif_word}\n”;
    #count word position frequency
    $$motif_positionref{$motif_word}[$s_i]++; #length – position =
    distance from the ATG
    #print “$motif_word $motif_position{$motif_word}[$s_i] $s_i\n”;
    #count the word frqueny for a sequence
    $$motif_each_seqref{$name}{$motif_word}++;
    #print “$$motif_each_seqref{$name}{$motif_word}\n”;
    $motif_word=;
    }

    print “>>Name:$name \n”;
    for my $word (sort
    {$$motif_each_seqref{$name}{$b}$$motif_each_seqref{$name}{$a} }
    keys %{$$motif_each_seqref{$name}} )
    {
    # print “>>$word $name $$motif_each_seqref{$name}{$word}
    $$motifref{$word}\n”;
    }
    #print “\>$name\n$seqh1{$name}”;

    ========================

    =How to build a hash of array with PUSH
    my @a=qw(0 1 2 3 4 5 6 7);
    my @b=qw(a b c d e f g);
    for my $i (@a)
    {
    push @{$hash{$i}},$b[$i]; #add an element at the end of the array
    }

    for my $i (sort keys %hash )
    {
    # $#{$hash{$i}}=> size of the array
    for my $j (0..$#{$hash{$i}})
    { print ” $i $j $hash{$i}[$j]\n”;
    }

    }

    ============
    Output
    $ perl hash.pl
    0 0 a
    1 0 b
    2 0 c
    3 0 d
    4 0 e
    5 0 f
    6 0 g
    7 0

    Perl Hash Howto

    http://www.cs.mcgill.ca/~abatko/computers/programming/perl/howto/hash/
    Best practices for subroutines

    http://www.oreilly.com/catalog/perlbp/chapter/ch09.pdf

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s