File Opening, Input, Output and Sorting

File Handles

open (FILE_HANDLE, “file.name”);

Open for reading:

open (SONGS, “song_list.txt”);

open (SONGS, “song_list.txt”) or die “Can’t open that file! Error is $!”;

 

Retrieve lines using < > :

open (SONGS, “song_list.txt”) or die “Can’t open that file!”;
for $line (<SONGS>) {
print $line;
}

 

Open for overwriting:

open (SONGS, “>song_list.txt”) or die “Can’t open that file!”;
# Be aware that any original contents are now gone!

 

Open for appending:

open (SONGS, “>>song_list.txt”) or die “Can’t open that file!”;
# The original contents are preserved, just appended onto.

 

Writing to the file:

open (SONGS, “>song_list.txt”) or die “Can’t open that file!”;
print SONGS “Stairway to Heaven\n”;
# Everything else is gone

 

Appending to the file:

open (SONGS, “>>song_list.txt”) or die “Can’t open that file!”;
print SONGS “Copacabana\n”;

 

File Operations

Steve Litt’s PERLs of Wisdom: PERL File Input, Output and Sorting

(also see the root of this “tips” area, Steve Litt’s Perls of Wisdom)

Formatting and Printing: sprintf()

Perl File Handling: open, read, write and close files at Perlfect Solutions

-note the use of the returned error code (which isn’t always an error), $!

-and the “current line” special variable $_

-and the operator <FILE>

Perl tutorial at tizag.com

The Kinder, Gentler File Opening Tutorial at Perldoc.perl.org

File Handling at DevelopingWebs.net

10 Tech Skills Heading the Way of the Dinosaur

Now here’s a headline to make us techies cringe: high tech skills that, well, won’t be skills (won’t be in demand) not far in the future. Part of me laughs at the notion that tech experts are going to face diminishing demand; every IT person I know is crushed under demand for their services. But part of me understands the anxiety that currently-employed techies feel: they’re running in place, or falling behind, or totally losing the battle to stay current with technologies.

That is our stock-in-trade: we’re the people who know how to build, maintain and secure a working infrastructure. And, oh yeah, teach our users how to use it. Oh, and constantly fold in all those must-have new technologies, whether it be Blackberry servers (how Y2K!) or iPhones using Exchange. Often we’re the first person in the office to have an Android phone, though not always. Even if we aren’t, the minute we see one in the workplace we know whose responsibility it will be: ours.

And now there’s cloud computing, and virtualization, and tablets, and mobile apps. We have Sharepoint sprawl, an toolbar in Word that half our users can’t stand, and web technologies moving so fast that any website with a team of fewer than a dozen is obsolete by the time you build it. There are new data protection laws (fortunately none in New Mexico), breach disclosure laws (ditto), federal standards in more flavors that ice cream, and users who flatly refuse to give a thought to security. Should we really be afraid of becoming obsolete?

Only if we can’t keep up.

And half of keeping up is getting ahead, even if just a little bit, of the curve. Do you at least glance through InfoWorld, InfoWeek, ComputerWorld, NetworkWorld or take-your-pick of IT trade publications? Do you have some degree of foresight of what’s coming? Because you need to. Even if you already know this, make sure your management knows it.

In the mean time, check out this article at GlobalKnowledge.com:

http://www.globalknowledge.com/articles/generic.asp?pageid=2885&country=United+States

And consider: will you really be sorry to see manual software installation and updates go the way of the buggy whip? Gee, you won’t be visiting 200 desktops to update Office. That’s a shame, no?

Beginning (X)HTML

Course/Class Number: 58111/46065
Class Title: 58111 (X)HTML: Beginning

Resources

http://www.elizabethcastro.com/html6ed/examples/

1. Getting Started

Introductions and announcements

IMPORTANT NOTE: Step by step instructions are in boxes.
Please perform every step before continuing.
Raise your hand and let me know if you have problems with any step!

About browsers

Example files

View page source: Ctrl > U or Command > U

Use Notepad in Windows, Textedit in Mac.

How to open a file with your choice of program

A. Open Firefox and keep it open for the rest of the class. Use ONLY Firefox.

B. Open the Resource link http://www.elizabethcastro.com/html6ed/examples/ in a separate tab.
Keep it open for the rest of the class.

C. Create a folder on your desktop named www

D. Open a window into that folder and keep it open for the rest of class.

E. Go back to Firefox. Save Castro’s first example file as index.htm

F. Open your text editor and keep it open for the rest of the class.

G. Right-click on the index.htm file in your www folder.
Choose “Open With.”
Choose “Firefox.”

H. Right-click on the index.htm file in your www folder.
Choose “Open With.”
Choose “Notepad” or “Textedit.”

 

2. (X)HTML [Ch. 1]

HTML vs XHTML

Attributes and Values

Boxes model

 

3. Files

File location

Filenames [p 34]

ABOUT: .htm vs .html

 

4. Essential Tags [Ch. 3]

Container tags

Inline tags

HTML vs. XHTML doctype declarations [p 56]

head [p 58]

title [p 60]

body

A. Create a new blank file in your text editor.

B. Save it as simple.htm

C. Use the <html>, <head>, <title> and <body> tags to create a properly nested HTML page.

D. Put the text “Hello World” inside the body of your new page.

REMEMBER: ANYTHING THE VIEWER SHOULD SEE MUST BE INSIDE THE BODY TAGS!

 

5. Block-level tags [Ch 3, p 62]

paragraphs <p>

<br> vs. <br /> [p 66]

headings <h1> – <h6> [p 61]

<div> [p 64]

<blockquote> [p 74]

comments [p 67]

preformatted [p 73]

<hr /> [p 101]

A. Use the <p>, <br />, <h1>, <blockquote> and <hr /> tags to create a properly nested HTML page.

 

6. HTML Standards

Flavors & doctypes [p 40]

xmlns attribute [p 56]

browser differences

 

7. Inline tags

<span> [p 65]

<b> vs. <strong> [p 70]

<i> vs. <em>

<u>

<big> or <small> [p 71]

<sub> or <sup> [p 76]

A. Use the <strong>, <em>, <u>, <big> and <small> tags to create a properly nested HTML page.

Perl String Comparisons and String Replacements

Strings and String Comparisons

Be careful which operators you use! Are you assigning, or testing?

$answer = "no";
if ($answer == "yes")
{
print "Answer is Yes.\n";
}

Exercise: do test operatiors make a difference?
Test this code:

$i = 11;
if ($i == “11”) { print “Equivalent numbers.\n”; }
if ($i eq “11”) { print “Equivalent strings.\n”; }

Getting Parts of Strings: the substr() function

substr (the_string, starting_position, number_of_chars_to_get)

$x = “Medical experiments for the lot of you!”;
print substr($x, 0, 7); # “Medical”

Exercises:

Copy the two lines of code above into your working Perl file. Make sure they work.

What happens if you omit the number_of_chars_to_get? Try it, using the string above.

Select the word “experiments” using substr() and copy it into a variable.

Select a substring using negative numbers to count from the right instead of the left:

$x = “Medical experiments for the lot of you!”;
print substr($x, -11, 3); # “lot”

Exercise:

Use substr() to print only the words “for the lot of you!”

Select a substring and assign a new value to it:

$x = “Program in Bash!\n”;
substr($x, 11, 4) = “Perl”; # $a is now “Program in Perl!”;
print $x;

Exercise:

Use the example above to assign the string “I’m in school to study biology” to $x, then select the word “biology” and replace it with

Splitting strings apart:

split (delimiter, the_string, [optional_max_number_of_results])

$s = “Welcome Back Kotter”;
@s = split(/ /, $s); # “Welcome” “Back” and “Kotter”

Note the use of a regular expression as the first argument.

Resources

See perldoc for more string functions: http://perldoc.perl.org/functions/substr.html

Formatting strings: http://www.tizag.com/perlT/perlstrings.php

Perl “if” Testing

if ($user eq "fred") 
{
print "Welcome in, Fred.\n";
}
elsif ($user eq "joe" || $user eq "jill")
{
print "Get out, $user\!\n";
}
else
{
print "No Admittance.\n";
}

unless ($age > 21 && $smile == "big") 
{
print "You're too young for me.\n";
}
else
{
print "How about a date?\n";
}

Note the “and” ( && ) and “or” ( || ) operators.

Resources

http://www.tizag.com/perlT/perlif.php

Perl Debugging, Warnings and Diagnostics

Debugging

Using print( ) functions in “if” blocks

Testing small pieces of code

Warnings

Either:

use warnings; # this is a "pragma," and is placed at the top of a script, below the shebang

or:

#!/usr/bin/perl -w # use the -w flag directly in the shebang

or:

perl -w myscript.pl

Diagnostics: Full Documentation

Either:

use diagnostics;

or:

perl -w myscript.pl

Resources

http://perldoc.perl.org/warnings.html

http://perldoc.perl.org/diagnostics.html

Perl “for” Loops

for $i (1, 2, 3, 4, 5, 6) {
  print "$i\n";
}

@integers = (1 .. 10);
$limit = 25;
for $i (@integers, 15, 21, 23 .. $limit) {
print “$i\n”;
}

%months_days = (Jan => 31, Feb => 28, Mar => 31, Apr => 30, May => 31, Jun => 30, Jul => 31, Aug => 31, Sep => 30, Oct => 31, Nov => 30, Dec => 31)

# You could use this instead:
@months = keys %months_days;

for $i (keys %months_days) {
print “$i has %months_days{$i} days.\n”;
}

These are great for looping through an array:

foreach (@myarray) {
  print;
}

#!/usr/bin/perl

# Calculate compound interest

# Prompt user for inputs
print “Enter the starting amount: “;
$start_amount = <STDIN>;

print “Enter the starting year: “;
$year = <STDIN>;

print “How many years? “;
$duration = <STDIN>;

print “Enter the annual percentage rate: “;
$apr = <STDIN>;

# Do some nice formatting: provide column heads:
print “Year”, “\t”, “Savings Balance”, “\t”, “Interest”, “\t”, “New balance”, “\n”;

# Calculate interest for each year.
# Note where this loop begins and ends:
for $i (1 .. $duration) {
print $year, “\t”;

$year++;

print $start_amount, “\t”;

# First try this one
# $interest = ($apr / 100) * $nest_egg;

# Then try it this way
$interest = int (($apr / 100) * $start_amount * 100) / 100;
print $interest, “\t”;

$start_amount += $interest;

print $start_amount, “\n”;
} # Loop ends here.

print $year, “\t”, $start_amount, “\n”;

Resources

http://www.tizag.com/perlT/perlfor.php

See similar examples at http://www.perl.com/pub/2000/10/begperl1.html

and http://ezinearticles.com/?Create-a-Compound-Interest-Calculator-in-Perl&id=2079734.