Converting String Scalar Number

[problem]

Converting a string scalar into a number.

[/problem]

[solution]

To convert a string scalar to a number, just add zero.

Sometimes Perl calculations get a bit screwed, when Perl decides it is dealing with a string – not a number.

[/solution]

[example]

Here is the code to fix it up:


print $scalar + 0;
$str+=0;

[/example]

[reference]

[tags]Scalars, Perl Coding School[/tags]

[/reference]

Convert String Array

[problem]

Converting a string into an array

[/problem]

[solution]

To convert a string to an array, use split.

[/solution]

[example]

Here we have a string, comprising of fields separated by commas.

So we just split on the comma, but this could also be spaces, etc.


$str="one,two,three";
@rr=split ',', $str;
print $#rr."n"; # outputs 2

[/example]

[reference]

[tags], Perl Coding School[/tags]

[/reference]

Convert String Hash

[problem]

Need to convert a string to a hash in Perl.

[/problem]

[solution]

Split can also be used to generate a hash, from a string.

In this instance split looks at two things, the key to value separator (in this case an equals sign) and the comma to separate groups.

[/solution]

[example]


$str="mouse=one,cat=two,dog=three";
%ash=split /,|=/, $str;

while(($k,$v)=each(%ash)) {
print $k."=>".$v."n";
}

Produces:

cat=>two
dog=>three
mouse=>one

[/example]

[reference]

[tags]Perl Coding School[/tags]

[/reference]

Word Counting

[problem]

You want to count the words in a file, using Perl.

[/problem]

[solution]

Obviously wc -w can be used for this under UNIX.

But fairly decent demo on the power of split and push! 🙂

[/solution]

[example]


while(<>) { push @words, split; }

print "found ".scalar(@words)." words in filen";

Works nicely, even over multiple lines:


[marcus@bree]/var/log/httpd% echo "test 1 23 4 n a b c" | perl -e 'while() {
chomp();
push @words, split;
}
print "found ".scalar(@words)." words in filen";
'
found 7 words in file

[/example]

[reference]

[tags]Perl Coding School[/tags]

[/reference]

Number Output Lines

[problem]

You want to number each line of output.

[/problem]

[solution]

Again other ways to do this with UNIX, such as grep -vn XXXX filename.

But sure there are times you want the line number, of standard input.

[/solution]

[example]


echo "testingntestingn123" | perl -e 'while(<>) {
chomp();
print($..": ".$_."n");

}'
1: testing
2: testing
3: 123

Or all on one line


echo "testingntestingn123" | perl -ane 'chomp();print($..": ".$_."n");'
1: testing
2: testing
3: 123

[/example]

[reference]

[tags]Automatic Numbering, Perl Coding School[/tags]

[/reference]

Perl Doc Usage

[problem]

You need help on a specific Perl function or module. Or just a general question.

[/problem]

[solution]

Perl installation comes with its own documentation system.

In this post, I’ll just cover how to invoke the various perl documentation.

Going forward I’ll add posts describing how to create the different types of Perl doco.

[/solution]

[example]

See different Perl areas under the documentation system
perldoc perl

Lookup function name
perldoc -f func name

Display source of module
perldoc -m mod name

Search perl FAQ
perldoc -q pattern

Search entire Perl installation
perldoc -r pattern

[/example]

[reference]

[tags]Perl, Tutorials, Help, Perl Coding School[/tags]

[/reference]

Importing AWK SED

[problem]

You have some way cool awk or sed scripts, but you want to use Perl.

[/problem]

[solution]

Easy. Perl comes with some excellent binaries, which will convert AWK or SED scripts to Perl. Excellent tool for learning Perl too!! 🙂

[/solution]

[example]

To convert from AWK to Perl:


a2p awkscript

To convert from Sed to Perl:


s2p awkscript


$ date | awk ' { print $(NF-1) } '
WST

$ cat > awkscript
{ print $(NF-1) }

$ date | awk -f awkscript
WST

$ a2p awkscript
#!/usr/bin/perl
eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
if $running_under_some_shell;
# this emulates #! processing on NIH machines.
# (remove #! line above if indigestible)

eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_0-9]+=)(.*)/ && shift;
# process any FOO=bar switches

$[ = 1; # set array base to 1
$, = ' '; # set output field separator
$ = "n"; # set output record separator

while () {
chomp; # strip record separator
@Fld = split(' ', $_, 9999);
print $Fld[$#Fld - 1];
}

$ a2p awkscript > perlscript

$ date | ./perlscript
WST

[/example]

[reference]

[tags]Perl, awk, sed, Perl Coding School[/tags]

[/reference]

Environmental Variables

[problem]

You want to set something once and have it remembered between UNIX settings.

[/problem]

[solution]

Set some variables. Simplest way to set environmental variables, is via the shell rc or profile scripts.

[/solution]

[example]

For example if your shell is zsh, use .zprofile or .zshrc. For bash it is .bashrc or .profile.

To invoke env vars from cron, use something like this:


30 * * * * zsh -c "(source $HOME/.zprofile; cmd ... ")

To source your .zprofile before command invocation. Additionally you can use a dot to source a script containing your vars:


. scriptname

[/example]

[reference]

[tags]UNIX, variables, shell scripting, Unix Coding School[/tags]

[/reference]

Counting Column Contents

[problem]

You want to count the contents of a column. Accumulatively totaling each number in a given column.

[/problem]

[solution]

To total up the contents of a given column, use following awk code:

[/solution]

[example]


nawk -v col=# ' { tot+=$col } END { print tot } '

In this example, do a du of the current directory and then total column one (1). Notice awk receives a variable on the column to sum up.


du -ks * | awk -v col=1 ' { tot+=$col } END { print tot } '
11168

[/example]

[reference]

[tags]UNIX, awk, nawk, gawk, Unix Coding School[/tags]

[/reference]

Regular Expression Matching NOT

[problem]

You want to find a line, not matching the pattern.

[/problem]

[solution]

I find this invaluable whilst editing crons. Or in vi – like this: [^x] where x equals the character you want to ignore.

[/solution]

[example]

For example after performing a crontab -e, wanting to skip comments.


crontab -e
/^[^#]

Also with sed – substituting tags, etc:


sed 's#]*>##g' filename

This says edit your crontab, match the next line that has a beginning, immediately followed by a character that is not a comment.

After performing this once, just type n – for the next match (i.e. next line that does not start with a comment).

[/example]

[reference]

[tags]UNIX, vi, regular expression, Unix Coding School[/tags]

[/reference]