1752383 Members
5568 Online
108788 Solutions
New Discussion юеВ

script help

 
joe_91
Super Advisor

script help

Could someone just tell me what the cshell script does and answer to the perl script?

#!/usr/bin/csh
set list = `ls`
foreach file ( $list )
set size = `wc -c $file`
if( (-f $file) && ($size[1] < 1000) ) echo $file
end
---------------------------------------------------

What will the following Perl script print out?
----------------------------------------------

#!/usr/bin/perl -w
use strict;

my @files = ('something.data',
'file.dat',
'data.txt',
'alpha.datdat',
'beta.a.dat');
my @list = ();

foreach my $file (@files) {
next if ( $file !~ m/\.dat$/i );
push(@list, $file);
}

foreach my $item (@list) { print "$item\n"; }
exit;

Please..could anyone help?

Thanks

Joe
4 REPLIES 4
Stephen Keane
Honored Contributor

Re: script help

The cshell script will echo the names of all files (not directories) in the current directory that have less than 1000 characters in them. It does not do a recursive list.


Peter Godron
Honored Contributor

Re: script help

Joe,
perl script:
create an array (@files) and populate with data
create an empty array(@list)

go through the entries in the files array
if the entry ends in a ".dat" append to list array

print each of the entries in the list array

Regards
H.Merijn Brand (procura
Honored Contributor

Re: script help

And the inefficient perl script can be written much shorter:

--8<---
#!/usr/bin/perl -lw
use strict;

my @files = qw(something.data
file.dat data.txt alpha.datdat beta.a.dat);
my @list = grep /\.dat$/i => @files;
print for @list;
-->---

the list @files is a list of predefined file names. @list is then stuffed with the names that match the file names that end on .dat (case insensitive), and that list is then printed to the standard output (your screen)

and reduce that to

--8<---
#!/usr/bin/perl -lw
use strict;

print for grep /\.dat$/i => qw(something.data
file.dat data.txt alpha.datdat beta.a.dat);
-->---

Which was probably an elaborate way to check what would happen on

# perl -lwe'print for grep m/\.dat$/i => <*dat*>'

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Peter Godron
Honored Contributor

Re: script help

Joe,
is this still a problem?
If not can you please identify the solution and close the thread, otherwise please update.
Thanks