Skip to content

Instantly share code, notes, and snippets.

@ynonp
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ynonp/faaaadeb8833cc14b9d8 to your computer and use it in GitHub Desktop.
Save ynonp/faaaadeb8833cc14b9d8 to your computer and use it in GitHub Desktop.
use strict;
use warnings;
use v5.18;
my %anagrams;
sub anagram {
my ( $word ) = @_;
my @letters = sort split //, $word;
$anagrams{"@letters"};
}
open my $fh, '<', '/usr/share/dict/words';
while(<$fh>) {
chomp;
next if ! /^[a-z]+$/;
my @letters = sort split //;
my $new_word = $_;
$anagrams{"@letters"} .= $new_word . ",";
}
close $fh;
print anagram('dad'), "\n";
print anagram('cat'), "\n";
print anagram('mouse'), "\n";
use strict;
use v5.16;
sub count_lines_in_file {
my ( $filename ) = @_;
open my $fh, '<', $filename
or die {
code => 1,
msg => "No such file: $filename",
};
my $count = 0;
while(<$fh>) {
$count++;
}
close $fh;
return $count;
}
# try the code block
eval {
print count_lines_in_file('/etc/asswd'), "\n";
};
# error is saved in $@
if ( $@ ) {
if ( $@->{code} == 1 ) {
};
print "warning: couldn't get line count...\n";
print $@;
}
print "--- the end ---\n";
use strict;
use warnings;
use v5.16;
print "Got args: @ARGV\n";
my %argv;
for my $word (@ARGV) {
$argv{$word}++;
}
my @uniq = keys %argv;
print "uniq = @uniq\n";
use strict;
use warnings;
use v5.16;
use List::MoreUtils qw/uniq/;
my @uniq = uniq @ARGV;
print "uniq = @uniq\n";
use strict;
use warnings;
use v5.16;
open my $pipe, '|sort -u';
for my $word (@ARGV) {
print $pipe $word, "\n";
}
close $pipe;
use strict;
use warnings;
use v5.16;
sub primes {
# return @result;
}
#
# BONUS
# takes a word and prints all other words
# from /usr/share/dict that are its
# anagram
# (made from the same letters)
sub anagrams {
}
# takes at least two arrays
# and returns
# sum(@a) - sum(@b)
sub diff_sum {
}
###########################
# Class examples
#
#
# returns 2x + 3y + 7z
sub example {
my ( $x, $y, $z ) = @_;
return 2 * $x + 3 * $y + 7 * $z;
}
# prints number of arguments
# passed to it
sub how_many_args {
my $count = @_;
print $count, "\n";
}
sub even_odd {
my ( $num ) = @_;
if ( $num % 2 == 0 ) {
print "even\n";
} else {
print "odd\n";
}
}
my @primes_till_13 = primes(13);
#!/usr/bin/perl
# Specify perl version best used with this script
use v5.16;
# Enforce modern coding style
use strict;
# 'a' + 7
use warnings;
##########
# Functions
# Variables + operators
# Input / Output
# Loops / If
#
my $name = 'ynon';
my $email = 'ynon@ynonperek.com';
my $number = 7;
print "hello world\n";
print "who are you?\n";
$name = <STDIN>;
print "Welcome, $name";
my $x = 10;
my $y = 7;
if ( $x < $y ) {
}
while ( $x < $y ) {
$x++;
}
for ( my $i=0; $i < 10; $i+=2 ) {
print "i = $i\n";
}
while(<STDIN>) {
chomp;
print length;
}
print for @primes;
my @primes = (3, 5, 7, 11, 13, 17);
for (@primes) {
print "$_ is a prime\n";
}
for my $num (@primes) {
print "$num is a prime\n";
}
foreach my $num (@primes) {
print "$num is a prime\n";
}
my %details = (
'bob@gmail.com' => 'Mr. Bob',
'ynon@ynonperek.com' => 'Ynon',
'john@walla.com' => 'Jane Doe',
);
print $details{'bob@gmail.com'};
use strict;
use warnings;
use v5.16;
$SIG{INT} = sub { ... };
sub dostuff {
local $SIG{INT} = sub { say "Oh no don't go" }
while(1) {
say "yo dude you're so good lookin";
sleep 5;
}
}
use strict;
use warnings;
use v5.16;
use List::Util qw/sum/;
sub is_prime {
my ( $n ) = @_;
for ( my $i=2; $i <= int(sqrt($n)); $i++ ) {
return if $n % $i == 0;
}
1;
}
sub primes {
my ( $target ) = @_;
grep { is_prime($_) } (1..$target);
}
#
# BONUS
# takes a word and prints all other words
# from /usr/share/dict that are its
# anagram
# (made from the same letters)
sub anagrams {
}
# takes at least two arrays
# and returns
# sum(@a) - sum(@b)
sub diff_sum {
my $size = shift;
my @a = splice @_, 0, $size;
my @b = @_;
sum(@a) - sum(@b);
}
my @l = (10, 20, 30);
my @k = (10, 25);
my $size = @l;
my $d = diff_sum($size, @l, @k);
print "diff sum = $d\n";
###########################
# Class examples
#
#
# returns 2x + 3y + 7z
sub example {
my ( $x, $y, $z ) = @_;
return 2 * $x + 3 * $y + 7 * $z;
}
# prints number of arguments
# passed to it
sub how_many_args {
my $count = @_;
print $count, "\n";
}
sub even_odd {
my ( $num ) = @_;
if ( $num % 2 == 0 ) {
print "even\n";
} else {
print "odd\n";
}
}
my @primes_till_13 = primes(13);
print "@primes_till_13", "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment