Skip to content

Commit

Permalink
Perl4 array scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
srobb1 committed Oct 16, 2012
1 parent 7980cb6 commit 96f2bc8
Show file tree
Hide file tree
Showing 15 changed files with 243 additions and 0 deletions.
27 changes: 27 additions & 0 deletions lecture_scripts/change_arrays.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/perl
use warnings;
use strict;

my $favorite_color = 'green';
my @colors = ('red', $favorite_color, 'cornflower blue', 5);

my $org = join ("--" , @colors );

## push changes the array
push (@colors , 'black');
my $new = join ("--" , @colors );

print "org: $org\n";
print "new: $new\n";

my $last = pop @colors;

# while tests for truth
# a list is true if it is not empty ( len > 0 )
# a list is false when it is empty ( len == 0)
while (my $last = pop @colors){
print "last is $last\n";
}

# example of scope and my
print "$last\n"; ## this will be 'black'
16 changes: 16 additions & 0 deletions lecture_scripts/favorite_colors.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/perl
use warnings;
use strict;

my $favorite_color = 'green';
my @colors = ('red', $favorite_color, 'cornflower blue', 5);

$favorite_color = 'black';
my $second = $colors[1];

print "fc:$favorite_color\tsecond:$second\n";

my $length = scalar @colors;
#my $length = length @colors; #this is wrong, returns 1
print "len = $length\n";

16 changes: 16 additions & 0 deletions lecture_scripts/join_array.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/perl
use warnings;
use strict;

my $favorite_color = 'green';
my @colors = ('red', $favorite_color, 'cornflower blue', 5);
my @colors2 = ('red', $favorite_color, 'cornflower blue', 5);

my $string = join ("\t" , @colors);
my $string2 = join ("--" , @colors , @colors2);

print $string , "\n";
print $string2 , "\n";
## @colors is still the same, not changed by join
print $colors[0] , "\n";

13 changes: 13 additions & 0 deletions lecture_scripts/loop_control.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/perl
use warnings;
use strict;

my @words = qw(I do not like green eggs and ham);

#foreach my $word (sort {uc($a) cmp uc($b)}@words){

foreach my $word (@words){
next if $word eq "and";
print "$word\n";
}

13 changes: 13 additions & 0 deletions lecture_scripts/loop_control.pl~
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/perl
use warnings;
use strict;

my @words = qw(I do not like green eggs and ham);

#foreach my $word (sort {uc($a) cmp uc($b)}@words){

foreach my $word (@words){
next if $word eq "and";
print "$word\n";
}

8 changes: 8 additions & 0 deletions lecture_scripts/map.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/perl
use strict;
use warnings;

my @array = qw (a f gf t ffffh j );
my @map_array =
map { my $elem = $_ ; length $elem } @array;
print "@map_array\n";
12 changes: 12 additions & 0 deletions lecture_scripts/print_array.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/perl
use warnings;
use strict;

my $favorite_color = 'green';
my @colors = ('red', $favorite_color, 'cornflower blue', 5);

## no quotes no spaces
## quotes, array will print with spaces
print "no quotes: " , @colors , "\n";
print "quotes: @colors\n";

9 changes: 9 additions & 0 deletions lecture_scripts/seq_array.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/perl
use warnings;
use strict;

my @seqs = qw ( ATGGTGCA TTTTTAA CCCGGG TTTATATATAT );

while (my $seq = shift @seqs){
print "seq: $seq\n";
}
25 changes: 25 additions & 0 deletions lecture_scripts/seq_for.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/perl
use warnings;
use strict;

my @seqs = qw ( ATGGTGCA TTTTTAA CCCGGG TTTATATATAT );

## length of the array is one more than the last index
for (my $i = 0 ; $i < scalar @seqs ; $i++){
print "seq: $seqs[$i]\n";
}

print "\n-------\n";

foreach my $seq (@seqs){
print "seq: $seq\n";
}

print "before while: @seqs\n";
print "\n-------\n";

##after this while loop, @seqs is empty, len = 0
while (my $seq = shift @seqs){
print "seq: $seq\n";
}
print "after while: @seqs\n";
25 changes: 25 additions & 0 deletions lecture_scripts/seq_for.pl~
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/perl
use warnings;
use strict;

my @seqs = qw ( ATGGTGCA TTTTTAA CCCGGG TTTATATATAT );

## length of the array is one more than the last index
for (my $i = 0 ; $i < scalar @seqs ; $i++){
print "seq: $seqs[$i]\n";
}

print "\n-------\n";

foreach my $seq (@seqs){
print "seq: $seq\n";
}

print "before while: @seqs\n";
print "\n-------\n";

##after this while loop, @seqs is empty, len = 0
while (my $seq = shift @seqs){
print "seq: $seq\n";
}
print "after while: @seqs\n";
18 changes: 18 additions & 0 deletions lecture_scripts/seq_foreach.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/perl
use warnings;
use strict;

my @seqs = qw ( ATGGTGCA TTTTTAA CCCGGG TTTATATATAT );

foreach my $seq (@seqs){
print "seq: $seq\n";
}

print "before while: @seqs\n";
print "\n-------\n";

##after this while loop, @seqs is empty, len = 0
while (my $seq = shift @seqs){
print "seq: $seq\n";
}
print "after while: @seqs\n";
18 changes: 18 additions & 0 deletions lecture_scripts/seq_foreach.pl~
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/perl
use warnings;
use strict;

my @seqs = qw ( ATGGTGCA TTTTTAA CCCGGG TTTATATATAT );

foreach my $seq (@seqs){
print "seq: $seq\n";
}

print "before while: @seqs\n";
print "\n-------\n";

##after this while loop, @seqs is empty, len = 0
while (my $seq = shift @seqs){
print "seq: $seq\n";
}
print "after while: @seqs\n";
14 changes: 14 additions & 0 deletions lecture_scripts/seq_split.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/perl
use strict;
use warnings;

my $seq = "ATGCGACCGGTTAA";
my @nts = split('',$seq);
print "seq/array len = " , scalar @nts , "\n";
print "@nts\n";
print join("--",@nts),"\n";

my @sorted_nts = sort (@nts);
print join("--",@sorted_nts),"\n";


20 changes: 20 additions & 0 deletions lecture_scripts/sorting.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/perl
use warnings;
use strict;

my $num1 = 10;
my $num2 = 2;
my $num3 = 100;
my $num4 = 5;
my $num5 = 0;

### bad idea ## my $result_2 = $num1 <=> $num2;

## good idea ##
my @nums = ($num1 , $num2 , $num3, $num4 , $num5);
my @sorted_num = sort {$a <=> $b} @nums;

print "@sorted_num\n";



9 changes: 9 additions & 0 deletions lecture_scripts/split.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/perl
use strict;
use warnings;

my $string = "I do not like green eggs and ham";
my @words = split("green",$string);
print "@words\n";
print join("--",@words),"\n";

0 comments on commit 96f2bc8

Please sign in to comment.