Re: script help (61 Views)
Reply
Super Advisor
joe_91
Posts: 445
Registered: ‎05-21-2002
Message 1 of 5 (61 Views)

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
Please use plain text.
Honored Contributor
Stephen Keane
Posts: 1,155
Registered: ‎11-24-2004
Message 2 of 5 (61 Views)

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.


Please use plain text.
Honored Contributor
Peter Godron
Posts: 4,470
Registered: ‎02-13-2002
Message 3 of 5 (61 Views)

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
Please use plain text.
Honored Contributor
H.Merijn Brand (procura
Posts: 6,185
Registered: ‎10-13-1997
Message 4 of 5 (61 Views)

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
Please use plain text.
Honored Contributor
Peter Godron
Posts: 4,470
Registered: ‎02-13-2002
Message 5 of 5 (61 Views)

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
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation