Loading ...
Sorry, an error occurred while loading the content.

extracting substrings

Expand Messages
  • diheptal
    hi again, i ve been away from the group for a while. i m back with the following problem. [one day i ll be good enough to give advice as well.. maybe...] sorry
    Message 1 of 2 , Jul 30, 2003
    • 0 Attachment
      hi again,

      i've been away from the group for a while.
      i'm back with the following problem.
      [one day i'll be good enough to give advice as well.. maybe...]

      sorry if it is a long question.

      also i had some help with the script [thanks hans g.],
      so there are things in it i might not understand.


      #########

      this script below - what it WILL do.

      it will take a user entered string.
      it will search for it in a text file.
      if it is found it will return it.
      it will overwrite with white space everything else.
      it will preserve the format of the file.

      so, if my text is: 'hghghg wayne ggdg wayne'

      and i enter my name it will make:

      ' wayne wayne'

      ##############

      this script below - i WANT it to do.

      so if my text were [ok, unlikely, but...]

      'gggjwakjkyn lkle'

      again, if i enter my name, i'd want to get:


      ' wa yn e'

      so 1. i have to break up my user string into sub parts
      and 2. search for them within the longer string.
      trouble is i can try to create a sub string:

      $a = substr($re, 0, 2);

      and add it to my script:

      if (s/(.*?)($a)/@{[repl($1)]}$2/gi) {

      this will find 'wa' and will keep the formatting.

      but if i try to add further sub strings to get the rest -
      it goes seriously crazy and starts finding unexpected parts of my text.


      [the problem of only wanting to find them once,
      preferably within at most a couple of
      lines ... er, i haven't thought about- i might ask this later -
      if anyone can help me fix the problem above first...]

      here's my script..

      ###########

      #!/usr/bin/perl -w


      open(F1, "C:\\Windows\\Desktop\\Wayne\\name.txt") || die;
      open($out_file, ">C:\\Windows\\Desktop\\Wayne\\name2.txt") || die;


      print 'search for what? ';
      chomp($re = <STDIN>);


      sub repl { return " " x length $_[0]; }

      while (<F1>) {
      chop;
      if (s/(.*?)($re)/@{[repl($1)]}$2/gi) {


      substr($_, $+[0]) = "";
      } else {

      $_ = "";
      }
      s/^\s/ /g;

      print $out_file "$_\n" or die "Cannot write to `$out_file': $!";
      }

      close F1;
      close $out_file;

      ##################

      thanks

      wayne
    • Gordon Stewart
      Hi there - just a question that popped into my mind earlier on... When I am using perl modules, is it more economical / faster processing, to only use them
      Message 2 of 2 , Jul 30, 2003
      • 0 Attachment
        Hi there - just a question that popped into my mind earlier on...

        When I am using perl modules, is it more economical / faster processing, to
        only 'use' them all at the beginning of the script, or as each instance I
        need them ?
        (i'll only need them once., on a certain condition (each time the script is
        run..)


        EG :-

        top of script

        use moduleone;
        use moduletwo;
        use modulethree;

        (if condition ){
        process data, using module one
        }

        (if condition ){
        process data, using module two
        }

        (if condition ){
        process data, using module three
        }


        OR :-

        is it better to go this way :-


        (if condition ){
        use moduleone;
        process data, using module one
        }

        (if condition ){
        use moduletwo;
        process data, using module two
        }

        (if condition ){
        use modulethree;
        process data, using module three
        }


        If the data matches one criteria, it will not match any other criteria.. -
        So is it worth while 'loading' / 'using' each module / prepare it for use -
        If its likely not to be used ??


        G.
      Your message has been successfully submitted and would be delivered to recipients shortly.