Pablo Fischer wrote at Fri, 25 Jul 2003 13:49:55 +0000:

> I have a string (its an array, the array has the values that $ftp->ls("dir) 
> returns), so $array[1] contains this:
> -rw-r--r--   1 pablo    pablo       18944 Jul 16 21:14 File28903.zip
> 
> What I would like to do is: get the date (Jul 16), the time (12:14) and the 
> FileName,  each one if a different array, like..
> 
> my ($date, $time, $fname) = $array[1];
> 
> I know that I could to it with split, I solved the problem with
> 
> split("       ",$array[1]);

You should also consider to use
my @col = split " ", $array[1];
# or
my @col = split /\s+/, $array[1];

Allthough the first method looks very similar to your one,
splitting for one blank is a special case that splits at all whitespaces
doing nearly the same as the second line (but leading whitespaces are
ignored).

For further details, please read
perldoc -f split

> However I would like to know if I can separeate the original string into 
> columns, so I could access the values like:
> 
> $date = $col[6]+$col[7];

The addition seems to be the wrong opparator, as you want mainly
concatenate the month with the day of month. Use one of the next methods:
my $date = "@col[6,7]";
my $date = "$col[6] $col[7]";
my $date = join " ", @col[6,7];

> $time = $col[8];
> $fname = $col[9];

However, there would also be a regexp solution:

my ($date, $time, $fname) = 
   $array[1] =~ /(\w\w\w \d?\d) (\d?\d:\d\d) (\S+)$/;

(I don't have the feeling, that the solution is really better, but I find
it always good to know that there are more than one ways to do it)


Greetings,
Janek

PS: In any case, I would recommend you to give the variables a better
name. $array[1] is not very self explaining. What about $file_listing[1].

I personally, also found it good not play around with magic numbers like
6, 7, 8, 9 in my source code, unless it is very obviously what they are
doing. In your case it might be an idea to declare some constants that are
selfexplicating:
use constant MONTH_COL => 6;
use constant DAY_COL   => 7;
use constant TIME_COL  => 8;
use constant FNAME_COL => 9;
Than you can write e.g.:
my $date = "@col[MONTH_COL, DAY_COL]";
what is much easier to maintain on a long run.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to