 |
 |
 |
 |
#102629 - 04/22/01 12:57 AM
Regular Expressions
|
Junior Member
Registered: 04/19/01
Posts: 8
|
Ok heres the deal, I am trying to parse the 'LIST' output of any unix directory. A file entry goes something like this: drwx------ 5 anpatel students 512 Jan 13 2000 Desktop Simple way of getting each field is: my @properties = split(/s+/,$line); But this code has one specific flaw. What if the file was "New Text Document.txt", there would be no way saying that $properties[9] was the full name of the file. I thought of improving the code: my @properties = split( /s+/, $item ); my ( $perms, $lCount, $owner, $group, $size, $mod_mon, $mod_date, $mod_time ) = @properties; my $file = join( "", @properties[8.. $#properties] ); But $file will be "NewTextDocument.txt" because the &join will compine all the rest of array elements with "". I was looking for a perfect solution if some one could offer, of-course i am on the hunt. But lets see if some one figures out before me  hehehe Thanks for reading this!
|
|
Top
|
|
|
|
 |
 |
 |
 |
 |
 |
 |
 |
#102637 - 05/09/05 02:48 PM
Re: Regular Expressions
|
I type Like navaho
Registered: 03/10/00
Posts: 25580
Loc: Texas
|
|
|
Top
|
|
|
|
 |
 |
 |
 |
 |
 |
 |
 |
#102639 - 05/09/05 03:34 PM
Re: Regular Expressions
|
I type Like navaho
Registered: 03/10/00
Posts: 25580
Loc: Texas
|
Still have questions, but that at least tells me the reasoning behind the answer I can probably write it using the example posted, I'll write back when I have questions. Basically we readdir then use stat to be able to print the file size ($size) and date ($mtime). $filename gives me the file's name? Would something like this be in the right direction? use File::stat;
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks) = stat($filename); then use $filename, $mtime, and $size for the data I'm looking for?
|
|
Top
|
|
|
|
 |
 |
 |
 |
 |
 |
 |
 |
#102645 - 05/10/05 08:16 AM
Re: Regular Expressions
|
I type Like navaho
Registered: 03/10/00
Posts: 25580
Loc: Texas
|
|
|
Top
|
|
|
|
 |
 |
 |
 |
 |
 |
 |
 |
#102646 - 05/10/05 08:52 AM
Re: Regular Expressions
|
Addict
Registered: 05/29/00
Posts: 1820
Loc: Istanbul, Turkey
|
no problem... but you need to swap "Date:" "Size:" texts  and... I can give you a byte converter sub fsize {
my $size = shift or return '0 byte';
return sprintf("%1.2f %s",$size / 1024 / 1024 / 1024, " GB" ) if($size >= 1024*1024*1024);
return sprintf("%1.2f %s",$size / 1024 / 1024 , " MB" ) if($size >= 1024*1024 );
return sprintf("%1.2f %s",$size / 1024 , " KB" ) if($size >= 1024 );
return sprintf("%1.2f %s",$size , " Byte") if($size < 1024 );
return "$size Byte";
}so, you can use it like: opendir(UPLOADS, "$opt{dir}/$user_number") or die "Can not opendir($opt{dir}/$user_number): $!";
my @uploads = readdir(UPLOADS);
closedir(UPLOADS);
my($size, $mtime);
foreach my $filename (@uploads) {
next if $filename =~ /^./; # ignore anything that starts with a dot. like "." ".." ".htaccess"
next if -d "$opt{dir}/$user_number/$filename"; # ignore directories
($size, $mtime) = (stat "$opt{dir}/$user_number/$filename")[7,9] or die "Error: $!";
##output it
$size = fsize($size);
print qq(
<tr valign="top">
<td bgcolor="$vars_style{AltColumnColor2}" align="center" width="20"><input type="checkbox" name="$key" value="include" checked="checked" /></td>
<td bgcolor="$vars_style{AltColumnColor2}"><b><font size="$vars_style{FDTextSize}">$filename</font></b></td>
<td bgcolor="$vars_style{AltColumnColor2}" align="center" valign="middle">$size</td>
<td bgcolor="$vars_style{AltColumnColor2}" align="center" valign="middle">$mtime</td>
</tr>
);
}
|
|
Top
|
|
|
|
 |
 |
 |
 |
|
|