[plug] A question for perl hackers
Matthew Robinson
matt at zensunni.org
Thu Aug 22 12:29:27 WST 2002
> I am trying to grab the filesize in bytes from a listing like:
>
> 310272 Aug 21 22:18 inbox
> 5193576 Jul 7 09:45 lyndon
> 0 Aug 12 09:58 mailing lists
> 32720 Jun 10 20:10 mchoice
> 46513 Aug 16 16:18 netvigator
> 0 Aug 21 20:04 outbox
> 10069208 Aug 21 20:04 sent-mail
> 2377093 Aug 21 19:33 trash
>
> As I step through an array with foreach I would like to assign the
filesize
> in bytes (the first number) to a variable. I need to match:
>
> (1) zero or any number of spaces occuring at the beginning of the string:
> ^ * (a guess)
>
> (2) any number of continuous digits:
> [0-9*] (even bigger guess)
>
> (3) followed by a single space
>
> Putting that all together I get:
> /^ *[0-9*] ./ (probably wrong!)
This can be written better as:
/^\s*(\d+)/
which will match the beginning of the line (^) followed by 0 or more (*)
whitespace characters (\s), capturing the value between the brackets (), one
or more (+) digits (\d).
You can then put this into a loop as follows:
#!/usr/bin/perl
use strict;
use warnings;
while(<>) {
my($filesize) = (/^\s*(\d+)/);
print($filesize);
}
A regular expression that contains capturing brackets will return the values
for each set of capturing brackets in a list, therefore we can store the
captured value by assigning into a variable. This script will read the
data from STDIN so you would call it as follows:
perl script.pl data_file
Hope this helps,
Matt
P.S. If you are beginning to learn perl then the most important thing to
remember is to 'use strict;' and 'use warnings;' (as in the script above).
Strict enforces a number of rules, most notably you must declare all
variables before you use them, in the script above I had to use my to
declare the variable $filesize.
--
s&&!msfQ!&&s&$&utvK&&s&(Q)&\1!sfiupoB&&s&^&reverse Ibdlfs&e&s&^&#
&&s&$&#!uojsq&&s&(.)&chr(ord($1)-1)&ge&s&(.*)&reverse $1&see
More information about the plug
mailing list