The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Perl Programming
|
yet another regex question
Discuss yet another regex question in the Perl Programming forum on Dev Shed. yet another regex question Perl Programming forum discussing coding in Perl, utilizing Perl modules, and other Perl-related topics. Perl, the Practical Extraction and Reporting Language, is the choice for many for parsing textual information.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

August 4th, 2000, 10:37 AM
|
 |
SwollenMember
|
|
Join Date: Jun 2000
Location: the master control
Posts: 264
Time spent in forums: 13 h 14 m 57 sec
Reputation Power: 13
|
|
|
$_ = "something";
$' =~ /some/;
this will give me thing for $'
however, how can I do this without using special variables?
|

August 4th, 2000, 04:09 PM
|
|
Contributing User
|
|
Join Date: Aug 2000
Location: Indiana
Posts: 614
  
Time spent in forums: 4 h 49 m 49 sec
Reputation Power: 14
|
|
|
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).
|

August 5th, 2000, 08:17 PM
|
 |
SwollenMember
|
|
Join Date: Jun 2000
Location: the master control
Posts: 264
Time spent in forums: 13 h 14 m 57 sec
Reputation Power: 13
|
|
|
actually i am trying to steer clear of the special variables (ie $_). My goal was to find a way not to use them.
|

August 5th, 2000, 09:06 PM
|
|
Contributing User
|
|
Join Date: Aug 2000
Location: Indiana
Posts: 614
  
Time spent in forums: 4 h 49 m 49 sec
Reputation Power: 14
|
|
|
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).]
|

August 6th, 2000, 11:46 AM
|
 |
SwollenMember
|
|
Join Date: Jun 2000
Location: the master control
Posts: 264
Time spent in forums: 13 h 14 m 57 sec
Reputation Power: 13
|
|
|
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"
)
|

August 6th, 2000, 02:59 PM
|
|
Guest
|
|
Posts: n/a
Time spent in forums:
Reputation Power:
|
|
|
$word = "something";
$split = substr($word,0,4);
print "$splitn";
No regex here at all.
|

August 6th, 2000, 06:08 PM
|
 |
SwollenMember
|
|
Join Date: Jun 2000
Location: the master control
Posts: 264
Time spent in forums: 13 h 14 m 57 sec
Reputation Power: 13
|
|
|
freebsd...you have solved my woes once again. Thanks!
|

August 7th, 2000, 10:06 AM
|
|
Contributing User
|
|
Join Date: Aug 2000
Location: Indiana
Posts: 614
  
Time spent in forums: 4 h 49 m 49 sec
Reputation Power: 14
|
|
|
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?
|

August 7th, 2000, 10:40 AM
|
|
Junior Member
|
|
Join Date: Aug 2000
Location: Charleston, SC, USA
Posts: 10
Time spent in forums: < 1 sec
Reputation 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...
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|