get a part from a $var in perl

Hi,

I have a file which has list of files with some other info too.. say..

1/1/2008./to/path/filename1.:
...
...

something like that.. can anyone tell me how can i just open the file and read the contents and then cut just the filename1 and write it to an @array..

thanks

Is this exactly how the lines of the file are?

1/1/2008./to/path/filename1.:

which part of that is the part you need to open and read the file?

Here is my problem.. I have a file say .. test.lst .. Now i need to open the test.lst file which has contents which looks like ..

1/1/2008 00:00:00 /to/path/filename1:
1/1/2008 00:00:00 /to/path/filename2:
..
..

i need filename (filename1, filename2 ....) part in this line and store it in @array

Thanks

See how well this works:

my @array;
open (FH, 'yourfile') or die "$!";
while(<FH>){
   chomp;
   tr/://d;
   push @array, (split("/"))[-1];
}
close FH;
print "$_\n" for @array;

If you get too much garbage in the array you will need to use some filtering to get only the lines with a filename in them.

yay!! it worked!.. thanks a lot .. really appreciate your replies on time..:slight_smile:

a small problem as i said the contents of the file looked like
1/1/2008 00:00:00 /to/path/filename1:
1/1/2008 00:00:00 /to/path/filename2:
..

but it has one more additional character .. this way

1/1/2008 00:00:00 /to/path/filename1: 0
1/1/2008 00:00:00 /to/path/filename2: 0
..

im getting the filenames as

filename1 0
filename2 0
...

can you tell me how to get rid of this??

Thanks

try this instead:

my @array;
open (FH, 'yourfile') or die "$!";
while(<FH>){
   chomp;
   tr/://d;
   push @array, (split(/\s+/))[-2];
}
close FH;
print "$_\n" for @array;

this is the way it returns the filenames...

/to/path/filename1
...

can you explain me what does these two do?
tr/ ://d;
(split(/s\s+/))[-2]

when i read the name of the file from this filename list .. i need to check the directory and see if all the files are present or not... how is this possible??
thanks,

oops... I forgot you just wanted the filename. Try this instead of all previous suggestions:

my @array;
open (FH, 'yourfile') or die "$!";
while(<FH>){
   chomp;
   tr/://d;
   if (m#/(\S+)\s*\d*$#) {
      push @array, $1;
   }
}
close FH;
print "$_\n" for @array;

tr/://d; <-- removes all colons from the lines
(split(/s\s+/))[-2] <-- splits the line on spaces and returns the second to last field. It is called an array slice.

Please clarify, with some sample data if possible, what you need to do.

hey kevin.. thanks for your quickie replies.. appreciate it.. i figured out the filename check..so im fine.. thanks again!

You're welcome.