PDA

View Full Version : parsing filename/size


ignoranceisblis
07-01-2005, 07:48 PM
I'm writing a program to display the contents of a folder 3-dimensionally using the perl OpenGL and SDL modules. I need to be able to parse the command 'ls-s'. Assuming the output will look about like this:

6948 05 Did My Time.mp3
3420 05 Pennsylvania Is.mp3
7560 05-insane_clown_posse-red_neck_hoe-rfx.mp3
4240 05-r._kelly_and_jay-z-take_you_home_with_me-tas.mp3
3368 05-the_alchemist_feat._the_game-when_u_hear_that-whoa.mp3
4288 06 - Insane Clown Posse - Run!.mp3
6340 06 Alone I Break.mp3
3572 06 Nervous & Werid.mp3

with size on the left and then file names on the right. How can I parse this? Normally I would replace spaces with commas and parse that but the file names themselves have spaces so that won't work. Any Ideas?

Aradon
07-01-2005, 08:10 PM
I'm not sure if this is the best way but why don't you use the split command anyways with spaces and just take the first element in the array that comes back and use that as the size and then just concat the rest of the array together?

I don't know if this would work but something like this:


my @output;
my $line;

open(LS, "ls -l |");
while($line = <LS>)
{
@output = split(' ', $line);
print "Size: $output[0]\n";
for(my $i = 1; $i < $#output+1; $i++)
{
print "$output[$i]";
}
print "\n";
}


Anyways, something like that. There's probably some fancy Regex to do it but I think in these terms not regex, heh

FishMonger
07-01-2005, 10:35 PM
There's no need to use filehandle or system call. Perl has built in methods for doing file tests which include getting the file size while maintaining portablity.
http://www.cs.cf.ac.uk/Dave/PERL/node69.html

#!/usr/bin/perl -w

while (<*>) {
next if -d; # skip directories
print "$_ \tFile size: \t", -s, $/;
}

FishMonger
07-01-2005, 10:44 PM
You may want to put it into a var for later use.

my %fsize;

while (<*>) {
next if -d; # skip directories
$fsize{$_} = -s;
}