#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Kindly decode the perl script


    Hi, i am new to PERL
    Kindly decode the below PERL script. It will be of great help.
    need to know whats exactly happening in the below script...

    #!/usr/local/bin/perl
    use strict;

    my $inFile = $ARGV[0];
    my $outFile = $ARGV[1];

    open (IN, "<$inFile") or die ("Cannot open input file $inFile: $!\n");
    open (OUT, ">$outFile") or die ("Cannot open output file $outFile: $!\n");

    while(my $line = <IN>)
    {
    chomp $line;
    my $str;
    if($line =~ /(.*)?(\/CLS .+?)\/.*/)
    {
    $str = $2;
    }

    $line =~ s/(~F:CKL~X:.+?)~A/$1$str~A/g;
    print OUT $line,"\n";

    }
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0
    Hi As per my understanding the script is:
    1. Opening two files provided in the command line input($ARGV[0] , $ARGV[1]).
    2. Reading each line in file one.
    3. Searching if the file has "/CLS somedata"
    4. replace ~F:CKL~X:.+? ~A with /CLS somedata ~A
    5. write it to the second file.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by Rahul6990
    Hi As per my understanding the script is:
    1. Opening two files provided in the command line input($ARGV[0] , $ARGV[1]).
    2. Reading each line in file one.
    3. Searching if the file has "/CLS somedata"
    4. replace ~F:CKL~X:.+? ~A with /CLS somedata ~A
    5. write it to the second file.
    thanks rahul...
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    Originally Posted by Rahul6990
    Hi As per my understanding the script is:
    1. Opening two files provided in the command line input($ARGV[0] , $ARGV[1]).
    2. Reading each line in file one.
    3. Searching if the file has "/CLS somedata"
    4. replace ~F:CKL~X:.+? ~A with /CLS somedata ~A
    5. write it to the second file.
    You're kinda close, but not quite. Steps 3 and 4 are not what you said above.

    HINT: If the line doesn't have /CLS somedata/..., then $str is not defined, but the undefined value is used in the replace string.

    Also look carefully at what's being replaced. It isn't exactly what you said above. Note that $1 is being used in the replacement expression above.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0
    What is $1 and $2 here?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    834
    Rep Power
    496
    In a regex such as:

    Code:
    if($line =~ /(.*)?(\/CLS .+?)\/.*/)
    $1 will contain whatever was matched by what is in the first set of parentheses and $2 whatever was matched by what is in the second set of parentheses.

    So, $2 will contain what was matched by (\/CLS .+?)

IMN logo majestic logo threadwatch logo seochat tools logo