[plug] A question for perl hackers

Richard Henry r.henry at murdoch.edu.au
Thu Aug 22 08:30:34 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

You don't need to match the space.

> Putting that all together I get:
> /^ *[0-9*] ./  (probably wrong!)

Assuming the above data is in an array called @lines:

foreach $line (@lines) {
	$filesize = $1 if ($line =~ /^\s*([0-9]+)/);
}

****
The problem is that I don't want to just match a pattern, I want to assign 
the match to a variable and this is what has me a bit lost.
****

That's what the brackets () do.  Unescaped brackets are "ignored" in the
regexp match, and if the match is successful, the contents of the brackets
are assigned to the variables $1, $2, $3 etc.  Therefore in my above code,
if the match is successful, whatever was matched in the ([0-9]+) section
will be assigned to $1.

Hope this helps explain it,
Rich.



More information about the plug mailing list