#1
  1. SwollenMember
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Location
    the master control
    Posts
    264
    Rep Power
    14
    $_ = "something";
    $' =~ /some/;

    this will give me thing for $'
    however, how can I do this without using special variables?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2000
    Location
    Indiana
    Posts
    614
    Rep Power
    15
    First of all, "$' =~ /some/;" wont give you anything. That's just a matching syntax... typically used in an if statement. $_=~s/some//; would give you $_="thing";

    2nd of all, you can set $_ = to anything you want. Example:

    $_="something";
    $var=$_;
    $var=~s/some//;
    print "$var";

    This would print "thing".

    3rd, and more important, you only have to use "special" variables when the information is pass from one sub routine to another, or another action that required passing variables on:

    &print('asdf');

    sub print {
    $var=$_[0];
    print "$var";
    }#end sub

    would print "asdf";

    when reading a file $_ is used for each line (I assume you knew that since that's the variable you used).
  4. #3
  5. SwollenMember
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Location
    the master control
    Posts
    264
    Rep Power
    14
    actually i am trying to steer clear of the special variables (ie $_). My goal was to find a way not to use them.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2000
    Location
    Indiana
    Posts
    614
    Rep Power
    15
    Then when you read a file, do it like this:

    ###################
    open(FILE, file.txt);
    @file=<FILE>;
    close(FILE);
    ###################

    now @file contains the content of file.txt... each element of the array is one line of the file:

    file.txt:
    ---
    line 1
    line 2
    ---

    $file[0] now equads "line 1", $line[1] now equals "line 2".

    [This message has been edited by JonLed (edited August 05, 2000).]
  8. #5
  9. SwollenMember
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Location
    the master control
    Posts
    264
    Rep Power
    14
    well, I am not trying to read input. maybe we don't understand eachother. All I wanted was to figure out a regular expression.

    ie.
    $word = "something";

    (regex here)
    (result:
    $split = "some"
    )
  10. #6
  11. No Profile Picture
    freebsd
    Guest
    Devshed Newbie (0 - 499 posts)
    $word = "something";
    $split = substr($word,0,4);
    print "$splitn";

    No regex here at all.
  12. #7
  13. SwollenMember
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Location
    the master control
    Posts
    264
    Rep Power
    14
    freebsd...you have solved my woes once again. Thanks!
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2000
    Location
    Indiana
    Posts
    614
    Rep Power
    15
    Bah, that only works for that example exactly. If had something other than "something", you would get random returns.

    You need to use regular expressions... what's wrong with them?
  16. #9
  17. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Charleston, SC, USA
    Posts
    10
    Rep Power
    0
    if you want to keep the regex approach alive.....

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre># reading from a handle
    open (HANDLE,"somefile");
    # instead of while (<HANDLE> ), do
    while ($line = <HANDLE> )
    {
    # now work with $line
    $match = 'some';
    $line =~ /(.*)$match(.*)/gi;
    $before = $1; # catch contents of first ()
    $after = $2; # catch contents of 2nd ()
    # do stuff with before or after or match
    }
    close HANDLE;
    [/code]


    'hope this helps...

IMN logo majestic logo threadwatch logo seochat tools logo