[EMAIL PROTECTED] wrote:
I will answer and ask all questions in one email! k.
8> foreach (<V4>) {
Is there any good reason to slurp the entire file into memory?
What would you suggest? I want to read the entire file via a filehandle.
I have plenty of system memory, therefore why not?
Why not indeed? There is no reason not to if that is what you really want
to do.
You are using the match in a boolean context so the /g option makes no
sense.
15> #$fa[$i++] = +(split)[5,6,7] if (m/f01(\d+)/gi );
If I do not use the /g modifier then it will not slurp the entire file or
all instances of F01, I tried it without /g and it did not work.
According to the data you posted, and that you had not modified the value in
$/, then $_ would only contain one instance of 'F01'. And even if it
contained all instances of 'F01' the match operator is used in boolean
(scalar) context and you are not assigning the list it produces nor are you
using the results from the capturing parentheses. And using or not using the
/g option has no effect on whether or not the file will be slurped.
When you say "it did not work" what exactly do you mean?
The value of $#fa is not the number of elements in the array, for that you
want to use the array in scalar context:
print "Now printing array count \t", scalar @fa, "\n";
Or:
print "Now printing array count \t" . @fa . "\n";
In my Learning Perl 2nd edition and Programming Perl 3rd edition, no where
does it say use
scaler @fa to get the element count, rather it says use $#fa. Is there
something I am unaware of or was $# deprecated recently?
Why is scaler @fa better/more correct than $#fa?
Because the array in scalar context will always be equal to the number of
elements in the array.
$ perl -le'
my @a = qw/ a b c d e f /; # six elements
print qq(Number of elements in "@a" = ), scalar @a;
print "Index of last element = $#a, contents = $a[$#a]";
{ local $[ = 5;
print qq(Number of elements in "@a" = ), scalar @a;
print "Index of last element = $#a, contents = $a[$#a]";
}
'
Number of elements in "a b c d e f" = 6
Index of last element = 5, contents = f
Number of elements in "a b c d e f" = 6
Index of last element = 10, contents = f
I have been told that my @a = ( ); is more correct than my @a, but now I am
confused b/c others are telling me otherwise???
Neither one is *more* correct, they both result in the same thing, it's just
that "my @a = ();" is assigning emptiness to something that is already empty.
It's sorta like saying:
( $a = 9 ) = 9;
The second assignment doesn't do anything more then the first assignment has
already done.
Personally, I like my @a = ( );
because it lets me know explicitly that this array is now initialized to 0
elements, plus its faster at compile time
I think that you misinterpreted the benchmark results that I posted.
From Jupiter :
my @tmp = split /\w+/;
You do realize that the characters 'F', '0' and '1' are included in the
character class \w which split() is removing? :-)
yeah I realized that typo too late :), I meant \s not \w but then plain
old my @tmp = split; is even better :)
But I thought spit by default separates on whitespace?
Yes it does. (spit??)
And F, 0 and 1 are
indeed apart of char classes \w and \d?
\w includes 'F', '0' and '1' while \d includes '0' and '1'.
Finally no one has answered my question what does (split)[-1] mean?
perldoc perldata
[snip]
A list value may also be subscripted like a normal array. You must put
the list in parentheses to avoid ambiguity. For example:
# Stat returns list value.
$time = (stat($file))[8];
# SYNTAX ERROR HERE.
$time = stat($file)[8]; # OOPS, FORGOT PARENTHESES
# Find a hex digit.
$hexdigit = ('a','b','c','d','e','f')[$digit-10];
# A "reverse comma operator".
return (pop(@foo),pop(@foo))[0];
See also the "Slices" section of perldata.
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>