[EMAIL PROTECTED] (Michael R. Wolf) writes:
> I'd like a few volunteers to take a test that I've put
> together for an "Introduction to Perl" class that I teach.
> If you are a beginner or recent "graduate" of an intro
> course and have a few minutes,
e of reusable
subroutines and methods. I often think, "How do I want to
return false/failure? There's only 4 of 'em, which one
makes most sense in this context? Now that I've figured out
the false/failure mode, the true/success value is much
easier".
--
Michael R. Wolf
All mammals learn by playing!
[EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
[EMAIL PROTECTED] (John W. Krahn) writes:
> "Michael R. Wolf" wrote:
> >
> > [EMAIL PROTECTED] (John W. Krahn) writes:
> > >
> > > Are you sure there are only three? :-)
> > >
> > > $ perl -le'
> > > print qq(undef is
[EMAIL PROTECTED] (John W. Krahn) writes:
> "Michael R. Wolf" wrote:
> >
[...]
> > Any value that is not false is true. What 3 values indicate
> > false?
>
>
> Are you sure there are only three? :-)
>
> $ perl -le'
> print qq(undef is
[EMAIL PROTECTED] (Peter Scott) writes:
> At 01:15 AM 2/2/2002 -0500, Michael R. Wolf wrote:
>
> >I don't usually give quizes at the end of class, but a
> >client of mine has requested it. (Required it in fact. No
> >test, no payment!)
> >[snip]
> >
ame of an internal member. Keep it balck-box like this
sub size_of_internal_encapsulated_member {
return scalar @arr;
}
Because this is a class method, it's not even necessary to
pull off the $self parameter. The purpose of the method is
just to encapsulate the implementation.
--
Michael R. Wolf
All mammals learn by playing!
[EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
1: open (INFILE, "report.txt" ;
2: while () {
3: $line =~ /\"(.+?)\"/ ;
4: $YourTextBetweenTheQuotes = $1 ;
5: # Do whatever you want
6: }
7: close (INFILE)
Seems that a WinDOS file system insinuated itself on your
Perl keywords
--
Michael R. Wolf
Al
==
| Regular Expressions |
==
Supply the repetition quantifier for these convenience quantifiers:
*{___,___}
+{___,___}
?{___,___}
Briefly describe (less than 5 words) these flags
/i
/g
/x
/o
/============\
|
gt; print ++($foo = 'a0'); # prints 'a1'
=> print ++($foo = 'Az'); # prints 'Ba'
=> print ++($foo = 'zz'); # prints 'aaa'
=>
=> The autodecrement operator, however, is not magical.
I guess by "within its range" imp
tter. I use all the tools I can get.
--
Michael R. Wolf
All mammals learn by playing!
[EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
[EMAIL PROTECTED] (Ronald Yacketta) writes:
> This works great with one exception... the two values in the sed need to be
> passed into the script...
>
> IE:
> changedbsid.pl OLDSID NEWSID filename(s)
>
> could be ran against 1 or more files at a time.
>
> I havethis which works like a champ o
[EMAIL PROTECTED] (Michael R. Wolf) writes:
> Other alternatives include
> #! /usr/bin/perl -w -i
> #! /usr/bin/perl -w -i .bak
> $^I = undef;
Oops! May have been misleading. Here's commentary on
alternatives.
$^I = ''; # in-place, but no backup
$^I
ple loop, it's not a big deal. Start
nesting the loops, doing a copule 'o conditionals, throw a
few "my" variables in one block (but try to use 'em in
another) and the value of well-formatted code becomes real
important.
--
Michael R. Wolf
All mammals learn
EDIT The current value of the inplace-edit
extension. Use undef to disable inplace
editing
--
Michael R. Wolf
All mammals learn by playing!
[EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
ferences) into what's more comfortable (arrays). Do it
often enough and you won't *need* the intermediate step any
more (like me) though you'll have the freedom to *choose*
either way (like me).
--
Michael R. Wolf
All mammals learn by playing!
[EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
handler;
If you merely "return" from the handler, the interrupt will
get caught, handled, but *not* exit your program. Some
times this is good behavior -- a C-c can do a "reset", for
instance.
Check out "stty -a" from the command line. C-c is probably
mapped to INT
his is in _list_ context.
> my @line =;
The original code was in _scalar_ context.
> > my $line = ;
--
Michael R. Wolf
All mammals learn by playing!
[EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
de wrong, you've
messed it up for good. Keep backups. This is a very
*powerful* statement. In the right hands it's very good,
else it's very bad
Something like this might work.
perl -e s/_VALUETEST/_QAP2/g -i original.scr
--
Michael R. Wolf
All mammals learn by playing!
[EMAIL PROTECTED] (John W. Krahn) writes:
> "Michael R. Wolf" wrote:
[buggy code deleted ...]
> $ perl -le'
> @fred = qw(1 3 5 7 9);
> sub total {
> my $sum;
> $sum += $_ foreach (@_);
> }
# undef from final foreach always returned!!
joy the learning exercise,
Michael
Answers below
|
|
|
V
|
|
|
V
|
|
|
V
|
|
|
V
|
|
|
V
# Readable.
sub total {
my $sum;
foreach my $num (@_) {
$sum += $num;
}
return $
LB/Religion-1.04/Religion.pm
--
Michael R. Wolf
All mammals learn by playing!
[EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
g to do a
full-blown RE to get all the special cases would be like
trying to herd cats. Don't try it but for the very simple
cases.
--
Michael R. Wolf
All mammals learn by playing!
[EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
ead perldoc
la, you must read perlfunc
ti, no thanks I'll have a Coke
that will loop us back to
do..
do...
do..
--
Michael R. Wolf
All mammals learn by playing!
[EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
I am canceling my own article.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
[EMAIL PROTECTED] (Mb) writes:
> I asking if there is any module to make some directives
> like : runing a window appli, waiting it finish and get
> the result of runing.
$results = qx(whatever.exe args);
$results = `whatever.exe args`;
--
Michael R. Wolf
All mammals learn b
a 1-level pushback, but it
generalizes to as deep as you need.
while (my $line = (pop @pushback || <>)) {
...
push @pushback, $line if (your_conditions_may_vary);
...
}
--
Michael R. Wolf
All mammals learn by playing!
[EMAIL PROTECTED]
--
To unsubscribe, e-mail:
a 1-level pushback, but it
generalizes to as deep as you need.
while (my $line = (pop @pushback || <>)) {
...
push @pushback, $line if (your_conditions_may_vary);
...
}
--
Michael R. Wolf
All mammals learn by playing!
[EMAIL PROTECTED]
--
To unsubscribe, e-mail:
ollowed by line-feed (what used to advance the paper),
but without the line-feed, the characters already on the
line remain.
--
Michael R. Wolf
All mammals learn by playing!
[EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
[EMAIL PROTECTED] (Jenda Krynicky) writes:
> From: "Michael R. Wolf" <[EMAIL PROTECTED]>
> > When dealing with "my", you can look at the line that
> > declares it, then backup to the smallest enclosing brace.
> > The variable belon
[EMAIL PROTECTED] (Leon) writes:
> and its happy&sad journey ends at the "the curly Heavenly
> gates =>}".
I like it! Appropriate for "eventualdeath" With a name
like that you should get real sharp with scoping.
--
Michael R. Wolf
All mammals
$b++;
> print "2nd while \$b = $b\n";
> };
> };
The outer one is a global variable: $main::b or $::b
The inner one is a lexically scoped variable.
When dealing with "my", you can look at the line that
declares it, then backup to the smallest
Joshua Colson <[EMAIL PROTECTED]> writes:
> @NEWLIST = grep(!/^$/, @LIST);
Is there a reason you're using UPPER CASE? It's not
*wrong*, it's just more perl-ish style to use lower case.
@newlist = grep(!/^$/, @list);
And it lets your Shift key last longer.... :-)
--
e).
There are two solutions to your bug...
# use $line
while ( my $line = ) {
next if ( $line =~ /^\s*$/ );
}
# use $_
while ( ) {
next if ( /^\s*$/ );
}
--
Michael R. Wolf
All mammals learn by playing!
[EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTEC
n't use) is at a higher
scope. Even though it's spelled the same, it's a different
variable.
Here's a good little example. It remembers the scalar
between invocations, but doesn't allow it to be accessable
outside the blocck that encloses the subroutine.
{
my $serial_num
k your data carefully. You may be able to get away with
this RE. Luck you if you can. If not, you have to use one
of the CSV splitting modules. The first line works, the
second one doesn't.
"a", "bcd, efg, h", "c"
a,"bcd, efg, h",c
--
Michael R. W
", "bcd, efg, h", "c"';
@words = parse_line(",", 0, $line);
foreach (@words) {
printf "%d. <%s>\n", $index++, $_;
}
Should print:
1.
2.
3.
--
Michael R. Wolf
All mammals learn by playing!
[EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
at you are missing, or at least know what
direction it is. People like helping people that are
helping themselves. Don't put up a post that reeks of "I'm
helpless". We all started out at "ignorant" and are still
there on some subject; "ignorant" merely m
that I vaguely remember. I use it less now that I have the
Perl CD, since it's searchable. But I still use the paper
books, and would not substitute the CD for the paper. They
complement each other very well.
--
Michael R. Wolf
All mammals learn by playing!
[EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
7;2'],
'kelty' => ['3'],
'biran' => ['4']
);
foreach $fn (keys %hash) {
printf "key: '%s'\n\tvalue(s): [%s]\n",
$fn, join ", " => @{$hash{$fn}};
}
--
Michael R. Wolf
All mammals learn by playing!
[EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
ute broken
software into broken software is something worth
remembering. We call it playing with code, but it's really
a code word for learning -- take a look at my signature
file, and have fun.
Computers have been a continual mental exercise, and larning
adventure for me.
Good luck on your ad
lue. If ARRAY is omitted,
the function shifts @ARGV (in the main program), or @_ (in
subroutines). See also unshift, push, pop, and splice. The
shift and unshift functions do the same thing to the left
end of an array that pop and push do to the right end.
--
Michael R. Wolf
All mammals lea
a scalar context.
$MyLen = @MyArray;
or
print "Length of array is: ", scalar(@MyArray)
--
Michael R. Wolf
All mammals learn by playing!
[EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
t use Control-g as the
delimiter. You can't do that here, however. I think the
character must be printable.
It's probably a moot point any way since it's only hard
coded for the discussion. A real program would read it in a
loop
while(defined($line = <>)) {
# whatever.
}
--
Michael R. Wolf
All mammals learn by playing!
[EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
set. That
backtick is way up in the corner -- I miss it a lot, and
have to stop my normal touch-typing to get into hunt-peck
mode. Who says emacs isn't modal -- I've got my home keys
in touch-mode and the other keys in hunt-mode. :-)
--
Michael R. Wolf
All mammals learn by playing
art to me was the m//g in scalar
context. The first time it was called (test 3) it returned
qw(one two three). The second time it was called (on the
same string) it started where it left off and returned
qw(four five six) which is used to assign in a list context
to the LHS. There being only on
hat delete works on hashes, splice works on arrays.
--
Michael R. Wolf
All mammals learn by playing!
[EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
"John W. Krahn" <[EMAIL PROTECTED]> writes:
> my $MBody = `who`;
> my $MBody = qx(who); # same thing
IMHO qx() is more readable. I often miss seeing the
back-ticks, or see them as single-quotes during a code skim.
--
Michael R. Wolf
All mammals learn by playing!
Help yourself and your readers out. Use some whitespace!!!
Let form follow function.
if (@ARGV == 2) {
chdir $ARGV[0];
my $testfile = $ARGV[1];
} else {
my $testfile = $ARGV[0];
}
--
Michael R. Wolf
All mammals learn by playing!
[EMAIL PROTECTED]
--
To unsubscribe, e-ma
"\n";
# Should be the same.
print join("++" =>
$user_name,
$attr_name,
$attrs{$user_name}{$attr_name}; # difficult
),
"\n";
}
}
--
Michael R. Wolf
All mammals learn by playing!
[EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
ot;Open
systems" is a *marketing* term to MS, not a *technical*
term.
[end rant]
--
Michael R. Wolf
All mammals learn by playing!
[EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
n:
Arrays are ordered.
Diamond operator is ordered.
Assigning diamond operator to array is ordered.
Rely on it -- it's reliable.
Code to it.
--
Michael R. Wolf
All mammals learn by playing!
[EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
Is there a html2text script that can be used to prevent the
writer from creating 2 versions? This reeks of maintenance
headaches.
> --_=_NextPart
> Content-Type: text/html;
> charset="iso-8859-1"
>
>
>
>
> HTML mail here
>
&
ay;
The scalar *is* necessary since print would otherwise
provide a list context.
--
Michael R. Wolf
All mammals learn by playing!
[EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
"Agustin Rivera" <[EMAIL PROTECTED]> writes:
> print $#array;
Off by one. That prints the last index, not the number of
elements.
print scalar @array
If print isn't your use, try:
$length = @array;
$last_index = $#array;
--
Michael R. Wolf
All
Thanks for your compatability,
Michael
--
Michael R. Wolf
All mammals learn by playing!
[EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
ot a LIST.
Therefore the following won't work.
while ($f = shift sort {$a<=>$b} @list) { # Wront!!!
}
I can two-line it.
@list = sort {$a<=>$b} @list;
while ($f = shift @list) {
}
Can it be one-lined?
--
Michael R. Wolf
All mammals learn by playing!
[EMAIL P
mputer" languages, but (lately) they have
more to do with how *we* think than how *computers* operate.
I think they should be called "thinking languages" because
they structure our thinking. Boole came up with such a
structure long before it could be executed by cams, tubes,
or silicon.
rs to wrangle up the argument I just presented
but this little ditty is easy to remember. It's what I
remember when I'm coding. I don't have time or gray matter
to spare to remember precedence tables.
Use the "word flavor" (i.e. "or") with words (like die):
open HANDLE, "file" or die "why";
Use the "symbol flavor" (i.e. "||") with symbols (like "<", "<="):
if ($x < 7 || $y <= 42 || $z > 99)
It kinda' parallels another simple rule I have for use in
comparison operators:
Use the "word flavor" (i.e. "le") with words:
if ("apple" le "bannana")
Use the "symbol flavor" (i.e. "<=") with symbols (a stretch to mean "numbers")
if (7 <= 42)
And of course, with respect to parenthesis: when in doubt,
whip 'em out. Extra parens never hurt. Missing ones
often do.
--
Michael R. Wolf All mammals learn by playing!
[EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
[EMAIL PROTECTED] writes:
> > "Michael R. Wolf" <[EMAIL PROTECTED]> said:
>
> > Since <> is used so often in a while loop to mean the
> > diamond operator, I prefer to spell out "glob" as follows.
> >
> >
> > while (gl
while (glob "sn.") {
print "$_\n";
}
Just a *preference*. They're both *right*.
--
Michael R. Wolf
All mammals learn by playing!
[EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
operators. The
doubles were introduced in a later version and have a much
richer (super)set of operators than the single-character
operators. I "parsed" the Perl [[:digit]] similarly.
Now it makes sense. I can remove my "special case" rule --
it's cosistent with what
my @slice = (I don't remember);
my ($hour, $minute, $second, $julian) = (time)[@slice]
--
Michael R. Wolf
All mammals learn by playing!
[EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
e 32..126
into zentara's printing program. Especially if it's to work
in different locales.
--
Michael R. Wolf
All mammals learn by playing!
[EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
ot;Running" (*with* the double quotes around it - *9* characters).
--
Michael R. Wolf
All mammals learn by playing!
[EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
lus side. The minus
side is that you don't have control over the open failure, so you have
to be happy with the built-in version.
This code is *different*, not *better* ---
my %data;
@ARGV = qw(/data/table1 /data/table2);
while(<>)
> my ( $time, $in, $out ) = split;
>
t.
($foo = "bar") =~ s/bar/goo/g;
($_ = "bar") =~ s/bar/goo/g;
Better? I think not (therefore I am not?)
Different, but not better.
--
Michael R. Wolf
All mammals learn by playing!
[EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
66 matches
Mail list logo