[plug] A question for perl hackers

James Bromberger james at rcpt.to
Wed Aug 21 23:07:38 WST 2002


On Wed, Aug 21, 2002 at 11:00:00PM +0800, Lyndon Kroker wrote:
> 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!)

/^\s*(\d+)\s(\w+)\s(\d+)\s(\d{2}):(\d{2})\s(.*)$/

my $size = $1;
my $name = $6;

# And now get the time., but there is no year, so assume this year...
use Time::Local;
my $months = { 'Jan' => 0, 'Feb' => 1 .....};
my $time = timelocal(0, $5, $4, $3, $month->{$2}, (localtime())[5]);


Read * as 'zero or more'.
Read + as 'one or more'.
Read \s as 'any space'.
Read \d as 'any digit'.
Read {2} as 'two of the last things'.
Read . as 'any char'.
Read \w as 'word characters'.
Anything in () is a back reference. Start with $1. 

> So, in the interest of efficency, I humbly ask for assistance.

Others will have differnt ways. Remember, TMTOWTDI.
There's more than one way to do it.


 Regards,
  James

-- 
 James Bromberger <james_AT_rcpt.to> www.james.rcpt.to
 Remainder moved to http://www.james.rcpt.to/james/sig.html
 The Australian Linux Technical Conference 2003: http://www.linux.conf.au/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 227 bytes
Desc: not available
URL: <http://lists.plug.org.au/pipermail/plug/attachments/20020821/f411a6d5/attachment.pgp>


More information about the plug mailing list