Categories
status

Array construction in R, Perl, and Python

Array construction in R, Perl, and Python

R:

data

Perl:

@data = (1, 2, 3)
@data[2..3]
%hash = ('a' =>; 'b')
$hash{a}

Python:

data = [1, 2, 3, 'a', 'b', 'c']
data[2:3]
tuple = (1, 2, 3, 'a', 'b', https://buycbdproducts.com  # immutable
dict = {'a': 'America', 'c': 'Canada'}
dict['a']
Categories
status

Perl one liner executing a shell command on…

Perl one liner executing a shell command on files.

e.g. copy all *.c files to new names, *.R.

perl -de 'foreach (@ARGV) {($new = $_) =~ s/c$/R/; system("cp $_ $new");}' *c
Categories
status

Select random contents from a file Sort sort…

Select random contents from a file

Sort

sort -R infile | head -n 3000

Perl

perl -n -e 'print if (rand > 0.1)' infile
Categories
status

Perl Getopt Long The module check wrong argument…

Perl Getopt::Long.

The module check wrong argument but does not check when no argument is provided.
In order to check if there is no argument as well as wrong argument,
check the argument also.

GetOptions( 'test=s' => \$option_test )
    or exit(1);

defined($option_test)
    or die("Missing argument -t");

http://www.perlmonks.org/?node_id=789974

Categories
status

Perl resources Perl Training Australia Perl Tips http…

Perl resources

Perl Training Australia – Perl Tips
http://perltraining.com.au/tips/

Modern Perl
http://onyxneon.com/books/modern_perl/modern_perl_letter.pdf

Categories
status

Rename multiple files on Linux If you are…

Rename multiple files on Linux
If you are lucky to have rename installed on your system, use it.
Rename is a Perl program and can take Perl regular expression.

rename 's/^comp/compare/' *pdf

Or copy the code below.

http://www.perlmonks.org/?node_id=632437


#!/usr/bin/perl -w
#
#  This script was developed by Robin Barker (Robin.Barker@npl.co.uk),
#  from Larry Wall's original script eg/rename from the perl source.
#
#  This script is free software; you can redistribute it and/or modify it
#  under the same terms as Perl itself.
#
# Larry(?)'s RCS header:
#  RCSfile: rename,v   Revision: 4.1   Date: 92/08/07 17:20:30 
#
# $RCSfile: rename,v $$Revision: 1.5 $$Date: 1998/12/18 16:16:31 $
#
# $Log: rename,v $
# Revision 1.5  1998/12/18 16:16:31  rmb1
# moved to perl/source
# changed man documentation to POD
#
# Revision 1.4  1997/02/27  17:19:26  rmb1
# corrected usage string
#
# Revision 1.3  1997/02/27  16:39:07  rmb1
# added -v
#
# Revision 1.2  1997/02/27  16:15:40  rmb1
# *** empty log message ***
#
# Revision 1.1  1997/02/27  15:48:51  rmb1
# Initial revision
#

use strict;

use Getopt::Long;
Getopt::Long::Configure('bundling');

my ($verbose, $no_act, $force, $op);

die "Usage: rename [-v] [-n] [-f] perlexpr [filenames]\n"
    unless GetOptions(
	'v|verbose' => \$verbose,
	'n|no-act'  => \$no_act,
	'f|force'   => \$force,
    ) and $op = shift;

$verbose++ if $no_act;

if (!@ARGV) {
    print "reading filenames from STDIN\n" if $verbose;
    @ARGV = <STDIN>;
    chop(@ARGV);
}

for (@ARGV) {
    my $was = $_;
    eval $op;
    die $@ if $@;
    next if $was eq $_; # ignore quietly
    if (-e $_ and !$force)
    {
	warn  "$was not renamed: $_ already exists\n";
    }
    elsif ($no_act or rename $was, $_)
    {
	print "$was renamed as $_\n" if $verbose;
    }
    else
    {
	warn  "Can't rename $was $_: $!\n";
    }
}

__END__

=head1 NAME

rename - renames multiple files

=head1 SYNOPSIS

B<rename> S<[ B<-v> ]> S<[ B<-n> ]> S<[ B<-f> ]> I<perlexpr> S<[ I<files> ]>

=head1 DESCRIPTION

C<rename>
renames the filenames supplied according to the rule specified as the
first argument.
The I<perlexpr> 
argument is a Perl expression which is expected to modify the C<$_>
string in Perl for at least some of the filenames specified.
If a given filename is not modified by the expression, it will not be
renamed.
If no filenames are given on the command line, filenames will be read
via standard input.

For example, to rename all files matching C<*.bak> to strip the extension,
you might say

	rename 's/\.bak$//' *.bak

To translate uppercase names to lower, you'd use

	rename 'y/A-Z/a-z/' *

=head1 OPTIONS

=over 8

=item B<-v>, B<--verbose>

Verbose: print names of files successfully renamed.

=item B<-n>, B<--no-act>

No Action: show what files would have been renamed.

=item B<-f>, B<--force>

Force: overwrite existing files.

=back

=head1 ENVIRONMENT

No environment variables are used.

=head1 AUTHOR

Larry Wall

=head1 SEE ALSO

mv(1), perl(1)

=head1 DIAGNOSTICS

If you give an invalid Perl expression you'll get a syntax error.

=head1 BUGS

The original C<rename> did not check for the existence of target filenames,
so had to be used with care.  I hope I've fixed that (Robin Barker).

=cut

Categories
status

Save a Perl oneliner to a script perl…

Save a Perl oneliner to a script.

perl -MO=Deparse -pe 's/(\d+)/localtime($1)/e'

http://stackoverflow.com/questions/2822525/how-can-i-convert-perl-one-liners-into-complete-scripts

Categories
status

Remove the first line in Perl $first line…

Remove the first line in Perl

$first_line = <>;

while (<>) {
    do_something();
}
Categories
status

The Minus File Again following the lead of…

The Minus File
Again following the lead of the standard shell utilities, Perl’s open function treats a file whose name is a single minus, “-“, in a special way. If you open minus for reading, it really means to access the standard input. If you open minus for writing, it really means to access the standard output.

UPDATE
========
The minus file is not working with 3-argument open.

Bryan> How can I use the “safe” 3-argument open and still be able to read off a
Bryan> pipe?

You don’t. 2-arg open has to be good for something.

And 2-arg open is perfectly safe if the second arg is a literal:

open OTHER, “<-" or die; open my $handle, "<-" or die; Don't let anyone tell you "Always use 3-arg open" unless they also footnote it with "unless you have no variables involved". -- Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.posterous.com/ for Smalltalk discussion

Categories
status

Usage of perlpod Suggested by Damian Co …

Usage of perlpod
Suggested by Damian Conway in Perl Best Practices.

Name
Usage
Description
Required arguments
Options
Exit status
Diagnostics
Configuration
Dependencies
Incompatibilities
Bugs and limitations
Author
License and copyright
Disclaimer of warranty