Nano's Blog

01/05/2010

The pop and push Operators

Filed under: programming — Tags: — nano @ 13:55

The pop operator takes the last element off of an array and returns it;

The converse operation is push, which adds and element (or a list of elements) to the end of array.

The first argument to push or the only argument for pop must be an array variable – pushing and poping would not make sense on a literal list.

#!C:\Perl\bin\perl.exe
use strict; # enabled
use warnings; 

my @array; 

@array  = 5..9;
my $fred   = pop(@array);  # $fred gets 9, @array now has (5, 6, 7, 8 )
my $barney = pop @array;   # $barney gets 8, @array now has (5, 6, 7)
pop @array;             # @array now has (5, 6). (The 7 is discarded.)

print $fred;
print "\n";
print $barney;
print "\n";
print @array;
print "\n";

my @others;
push(@array, 0);      # @array now has (5, 6, 0)
push @array, 8;       # @array now has (5, 6, 0, 8 )
push @array, 1..10;   # @array now has those ten new elements
@others = qw/ 9 0 2 1 0 /;
push @array, @others; # @array now has those five new elements (19 total)

print @others;
print "\n";
print @array;

Output

9
8
56
90210
56081234567891090210

- from Learning Perl 5E

How to assign String to list when use strict enabled?

Filed under: programming — Tags: , — nano @ 13:26

Don’t use “my” again and again to declare $rock[0], $rock[1] etc. Declare the array once (@rocks) once and use it.

you need to declare my @rocks and then not use my any more when referring to $rocks[xxx].
If you don’t know how many elements are going to be in there, you can use push to add new elements into the (initially 0-sized) array.

- from StackOverflow.com

#!C:\Perl\bin\perl.exe
# activePerl 5.8 based
use strict; # enabled
use warnings; 

my @rocks; 

($rocks[0], $rocks[1]) = qw/Hello World/;  

$rocks[2] = 'Tom';
$rocks[3] = 'Cat'; 

print $rocks[0];
print $rocks[1];
print $rocks[2];
print $rocks[3];

Cloudy Tags Plugin Installed

Filed under: collections — Tags: — nano @ 11:59

Cloudy Tag V1.0

http://wordpress.org/extend/plugins/cloudy-tags/

WP V2.9.1

The chomp Operator

Filed under: programming — Tags: — nano @ 10:31
#!C:\Perl\bin\perl.exe
use strict;
use warnings;

my $text = "a line of text\n\n"; # Or the same thing from <STDIN>
chomp($text);   # Gets rid of the newline character, get rid of the newline \n
chomp $text;    # chomp is acturally a function, get the value 1, get rid of another newline \n
print $text;

Output


D:\learning\perl>hello.pl
a line of text
D:\learning\perl>

More test, if we commented one chomp expression

#chomp $text;    # chomp is acturally a function, get the value 1, get rid of another newline \n

Output(include one newline)


D:\learning\perl>hello.pl
a line of text
(# newline)
D:\learning\perl>

More test, and if we commented both of the two chomp expressions

#chomp($text);   # Gets rid of the newline character, get rid of the newline \n
#chomp $text;    # chomp is acturally a function, get the value 1, get rid of another newline \n

Output (include two newlines)

D:\learning\perl>hello.pl
a line of text
(#newline0)
(#newline1)
D:\learning\perl>

Numberic and string comparison operators

Filed under: programming — Tags: — nano @ 09:31
ComparisonNumericString
Equal==eq
Not equal!=ne
Less than<lt
Greater than>gt
Less than or equal to<=le
Greater than or equal to>=ge

- from Learning Perl 5E

Output With Print

Filed under: programming — Tags: — nano @ 08:47
#!C:\Perl\bin\perl.exe -w
use strict;
use diagnostics;

print "The answer is ";
print 6 * 7;
print ".\n";

# Give print a series of values, separated by commas:
print "The answer is ", 6 * 7, ".\n";

Output

The answer is 42.
The answer is 42.

———-

#!C:\Perl\bin\perl.exe -w
use strict;
use warnings;
#use diagnostics;

my $fred = 'hello';
print "The name is $fred" . "\n"; # pring normally
print "The name is \$fred.\n";    # prints a dollar sign
print 'The name is $fred' . "\n"; # so does this

Output

The name is hello
The name is $fred.
The name is $fred

———-

#!C:\Perl\bin\perl.exe
use strict;
use diagnostics;

my $what = "brontosaurus steak";
my $n = 3;
print "fred ate $n $whats.\n";          # not the steaks, but the value of $whats

Output

Global symbol "$whats" requires explicit package name at D:\learning\perl\hello.
pl line 7.
Execution of D:\learning\perl\hello.pl aborted due to compilation errors (#1)
    (F) You've said "use strict vars", which indicates that all variables
    must either be lexically scoped (using "my"), declared beforehand using
    "our", or explicitly qualified to say which package the global variable
    is in (using "::").

Uncaught exception from user code:
        Global symbol "$whats" requires explicit package name at D:\learning\per
l\hello.pl line 7.
Execution of D:\learning\perl\hello.pl aborted due to compilation errors.

———-

#!C:\Perl\bin\perl.exe
use strict;
use warnings;

my $what = "brontosaurus steak";
my $n = 3;
print "fred ate $n ${what}s.\n";        # now uses $what
print "fred ate $n $what" . "s.\n";     # another way to do it
print 'fred ate ' . $n . ' ' . $what . "s.\n"; # an especially difficult way

Output

fred ate 3 brontosaurus steaks.
fred ate 3 brontosaurus steaks.
fred ate 3 brontosaurus steaks.

- from Learning Perl 5E

Scalar variables

Filed under: programming — Tags: — nano @ 08:42

Scalar variable names begin with a dollar sign followed by what we’ll call a Perl identifier: a letter or underscore, and then possible more letters, or digits, or underscores.

Upper and lowercase letters are distinct. Can’t start with a digit.

A scalar variable can hold only one value. But other types of variables, such as arrays and hashes, may hold may values.

- from Learning Perl 5E

Perl’s Built-in Warnings

Filed under: programming — Tags: — nano @ 08:15
$perl -w my_program

#!/usr/bin/perl -w

#!perl -w

#!C:\Perl\bin\perl.exe -w

With perl 5.6 and later, you can turn on warnings with a pragma.

#!C:\Perl\bin\perl.exe -w
use strict;
use warnings; # v5.6 and later

print "\n\n";
my $i = 4 * 'hello';
print $i;

Output

D:\learning\perl>hello.pl
Argument "hello" isn't numeric in multiplication (*) at D:\learning\perl\hello.p
l line 6.

0

if you get a warning message you don’t understand, you could get a longer description of the problem with the diagnostics pragma.
instead use warnings with use diagnostics
Out put:

D:\learning\perl>hello.pl
Argument "hello" isn't numeric in multiplication (*) at
        D:\learning\perl\hello.pl line 7 (#1)
    (W numeric) The indicated string was fed as an argument to an operator
    that expected a numeric value instead.  If you're fortunate the message
    will identify which operator was so unfortunate.

0

String Operators

Filed under: programming — Tags: — nano @ 07:56

. operator
a single period. String values can be concatenated with the operator.

x operator
a single lowercase letter x. A special repetition operator.

- from Learning Perl 5E

Sampe Code

#!C:\Perl\bin\perl.exe
use strict;
use warnings;

print "hello" . "world\n";       # same as "helloworld"
print "hello" . ' ' . "world\n"; # same as 'hello world'
print 'hello world' . "\n";    # same as "hello world\n"
print "\n";

print "4 x 5";
print "\n";
print '4 x 5';
print "\n";
print "4" x 5;
print "\n";
print 4 x 5;

print "\n\n";
my $i = 4 * 5;
print $i;

Output

helloworld
hello world
hello world

4 x 5
4 x 5
44444
44444

20

Single Quoted String Literals VS Double Quoted String Literals

Filed under: programming — Tags: — nano @ 07:39

Single-Quoted String Literals.

A single-quoted string literal is a sequence of characters enclosed in single quotes. The single quotes are not part of the string iteself.
‘hello\n’ # hello followed by backslash followed by n

Double-Quoted String Literals

It’s a sequence of characters, although this time enclosed in double quotes. But now the backslash takes on its full power to specify certain control characters.
“hello\n” # hello, and a newline

If you wish to use backslash escape(like \n to mean a newline character), you will need to use the double quotes.

- from Leaning Perl 5E

Sample code

#!C:\Perl\bin\perl.exe
use strict;
use warnings;

print '\'\\';
print "\n";
print 'Hello World!\n';
print "\n";
print "Hello World!\n";

Output

'\
Hello World!\n
Hello World!
« Newer PostsOlder Posts »

Powered by WordPress