473,503 Members | 1,739 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Explain this code please?

31 New Member
Expand|Select|Wrap|Line Numbers
  1.          for ($i = 0; $s < @FILENAME; $s++) {
  2.                 if (substr($FILENAME[$s], length($FILENAME[$s]) - 4) eq ".cgi" || substr($FILENAME[$s], length($FILENAME[$s]) - 3) eq ".pl") {
  3.                         $PERLFILE[$ss] = $FILENAME[$s];
  4.                         $ss++;
  5.                 }
  6.         }
I would say that this scirpt idenflies some file that is it perl script file or other, but can someone explain this line by line?
Oct 12 '08 #1
14 1538
über
31 New Member
damn i cant edit it but the script has error on the first line the $i is supposed to be $s
Oct 12 '08 #2
numberwhun
3,509 Recognized Expert Moderator Specialist
Um, where did you copy this code from? Either the $i needs to be a $s or the other way around. The for loop, as used, will never increment $i, so why use it?
Oct 12 '08 #3
über
31 New Member
Um, where did you copy this code from? Either the $i needs to be a $s or the other way around. The for loop, as used, will never increment $i, so why use it?
yeah i already fixed this in my first post and my friend gave it to me but he has gone to abroad so i cant contact him
Oct 12 '08 #4
KevinADC
4,059 Recognized Expert Specialist
Well, it loops through @FILENAME array, it uses the sunstr() function to get the last 4 or last 3 characters from each filename in the array and uses the "eq" operator to see what that value is (.cgi or .pl). It appears to assign that value to another array @PERLFILE if it does equal .cgi or .pl.

Looks like code written by a person that only knows the very basics or perl coding. It will work but there are probably better ways to do the same thing.
Oct 12 '08 #5
über
31 New Member
there are probably better ways to do the same thing
How would you do it?
Oct 12 '08 #6
KevinADC
4,059 Recognized Expert Specialist
Without knowing anything else about your existing program, one possibility:

Expand|Select|Wrap|Line Numbers
  1. my @PERLFILES = grep {/\.(cgi|pl)$/} @FILENAME;
Oct 12 '08 #7
über
31 New Member
I will post my friends whole script now
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. ## Made by Josh ##
  3.         opendir(DIR, ".");
  4.         @FILENAME = readdir(DIR);
  5.         closedir(DIR);
  6.          for ($s = 0; $s < @FILENAME; $s++) {
  7.                 if (substr($FILENAME[$s], length($FILENAME[$s]) - 4) eq ".cgi" || substr($FILENAME[$s], length($FILENAME[$s]) - 3) eq ".pl") {
  8.                         $PERLFILE[$ss] = $FILENAME[$s];
  9.                         $ss++;
  10.                 }
  11.         }
  12.         for ($s = 0; $s < @PERLFILE; $s++) {
  13.                 open(FILE,$PERLFILE[$s]);
  14.                 $fileindex = join("",<FILE>);
  15.                 close(FILE);
  16.                 if (index($fileindex, "## Made by Josh ##") == -1) {
  17.                         open(FILE,">>$PERLFILE[$s]");
  18.                         print FILE "## Made by Josh ##";
  19.                         close(FILE);
  20.                 }
  21.         }
  22.         closedir(DIR);
this script writes made by Josh in every script that havent got one yet in current directory. I would like to improve it by also writing to files that are in subdirectory. Now i know how to change the "readdir" part to read in all subdirs but the
Expand|Select|Wrap|Line Numbers
  1.          for ($s = 0; $s < @FILENAME; $s++) {
  2.                 if (substr($FILENAME[$s], length($FILENAME[$s]) - 4) eq ".cgi" || substr($FILENAME[$s], length($FILENAME[$s]) - 3) eq ".pl") {
  3.                         $PERLFILE[$ss] = $FILENAME[$s];
  4.                         $ss++;
  5.                 }
  6.         }
still would need changing and kevins code wouldnt work in this situation so any help?
Oct 12 '08 #8
KevinADC
4,059 Recognized Expert Specialist
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. ## Made by Josh ##
  3. use strict;
  4. use warnings;
  5. use File::Find;
  6.  
  7. my $start = 'start_directory';
  8. find({no_chdir => 1, wanted => \&wanted}, $start);
  9. print "Finished";
  10.  
  11. sub wanted {
  12.    if ($File::Find::name =~ /\.(cgi|pl)$/i) {
  13.       open (my $fh, ">>", $File::Find::name) or die "$!";
  14.       print $fh "\n## Made by Josh ##";
  15.    }
  16. }
Make sure to test it on some directory and files first to make sure it works OK. Will find all .cgi and .pl files in the "start_directory" and all sub directories of the "start_directory" and add your line.

Read the File::Find documentation for explanations of the code.
Oct 12 '08 #9
über
31 New Member
Thanks Kevin it works very well but i would also like to know how to check if the file already got the "## Made by Josh ##"
Oct 13 '08 #10
KevinADC
4,059 Recognized Expert Specialist
Thanks Kevin it works very well but i would also like to know how to check if the file already got the "## Made by Josh ##"
I leave that up to you to figure out.
Oct 13 '08 #11
über
31 New Member
I leave that up to you to figure out.
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. ## Made by Josh ##
  3. use strict;
  4. use warnings;
  5. use File::Find;
  6.  
  7. my $start = '.';
  8. find({no_chdir => 1, wanted => \&wanted}, $start);
  9. print "Finished";
  10.  
  11. sub wanted {
  12.    if ($File::Find::name =~ /\.(cgi|pl)$/i) {
  13.    open (my $fh, ">>", $File::Find::name) or die "$!";
  14.    my $filee = join("",<$fh>);
  15.    close(FILE);
  16.       if (index($fh, "## Made by Josh ##") == -1) {
  17.       open(FILE,">>$fh")
  18.       print $fh "\n## Made by Josh ##";
  19.       close(FILE);
  20.  
  21.    }
  22.    }
  23.    }
well i tried but it gives syntax error and the error says that something is wrong in line 18
Oct 13 '08 #12
KevinADC
4,059 Recognized Expert Specialist
OK, you gave it a try. I give you credit for the effort if not the execution. See how this works:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. ## Made by Josh ##
  3. use strict;
  4. use warnings;
  5. use File::Find;
  6.  
  7. my $start = '.';
  8. find({no_chdir => 1, wanted => \&wanted}, $start);
  9. print "Finished";
  10.  
  11. sub wanted {
  12.    my $fh;
  13.    if ($File::Find::name =~ /\.(cgi|pl)$/i) {
  14.       open ($fh, $File::Find::name) or die "$!";
  15.       while (<$fh>) {
  16.          if (/## Made by Josh ##/) {
  17.             return(0);
  18.          }
  19.       }
  20.       undef $fh;
  21.       open($fh, ">>", $File::Find::name) or die "$!";
  22.       print $fh "\n## Made by Josh ##";
  23.       undef ($fh);
  24.    }
  25. }
Oct 13 '08 #13
über
31 New Member
Ok thanks very much Kevin!
Oct 13 '08 #14
KevinADC
4,059 Recognized Expert Specialist
Ok thanks very much Kevin!
You're welcome
Oct 13 '08 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

10
1519
by: Randy | last post by:
I have asked a number of questions as a newbie trying to learn Access. Some folks have been very helpful with very direct and simple answers that help alot. Others give me answers with extensive...
12
3043
by: Sanjeev | last post by:
Output of followin program at Turbo C++ 3.0 is 7 ( Not 2 or 3). Please explain why ? //////////////////////////////////////////////// #include<stdio.h> #include<string.h> void main() {
22
2076
by: Jaspreet | last post by:
I was recently asked this question in an interview. Unfortunately I was not able to answer it and the interviewer made a decision on my C strengths (or weekness) based on this single question and...
10
2107
by: Jeff Boes | last post by:
I'm hoping there's someone here with experience in building the Visual Explain tool from Red Hat. I downloaded it and the J2 SDK, but when I attempt to follow the build instructions, I get messages...
9
1739
by: Abhishek | last post by:
Yet another option is to use pointers to arrays: int (*array4) = malloc(nrows * sizeof(*array4)); or even int (*array5) = malloc(sizeof(*array5)); Please explain the declaration of the...
118
6580
by: 63q2o4i02 | last post by:
Hi, I've been thinking about Python vs. Lisp. I've been learning Python the past few months and like it very much. A few years ago I had an AI class where we had to use Lisp, and I absolutely...
24
1765
by: muttaa | last post by:
i've a code snippet below: for(;0;) printf("Gud Morning"); when this is run,i get the "gud morning" message printed,eventhough the
1
1395
by: webinfinite | last post by:
HI, I ran across a code written by somebody else. Since I am new to C++, could you please explain what each of "const *" means in this code snippet as well as the last "const"? Tool const*...
3
1447
by: sathishc58 | last post by:
Hi All, Here is the code which generates Segmentation Fault. Can anyone explain why the third printf fails and the first printf works? main() { char ch={"Hello"}; char *p; ...
1
1559
by: kannan1986 | last post by:
now i am doing video stegnography in asp.net with c#. please give a source code or explain how do to that project. or Is there any source code available in websites. please send the source code...
0
7203
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
6993
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5579
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3156
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1514
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
737
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.