Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 4th, 2000, 10:37 AM
tron's Avatar
tron tron is offline
SwollenMember
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Location: the master control
Posts: 264 tron User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #2  
Old August 4th, 2000, 04:09 PM
JonLed JonLed is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2000
Location: Indiana
Posts: 614 JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level) 
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).

Reply With Quote
  #3  
Old August 5th, 2000, 08:17 PM
tron's Avatar
tron tron is offline
SwollenMember
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Location: the master control
Posts: 264 tron User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old August 5th, 2000, 09:06 PM
JonLed JonLed is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2000
Location: Indiana
Posts: 614 JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level) 
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).]

Reply With Quote
  #5  
Old August 6th, 2000, 11:46 AM
tron's Avatar
tron tron is offline
SwollenMember
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Location: the master control
Posts: 264 tron User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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"
)

Reply With Quote
  #6  
Old August 6th, 2000, 02:59 PM
freebsd
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
$word = "something";
$split = substr($word,0,4);
print "$splitn";

No regex here at all.

Reply With Quote
  #7  
Old August 6th, 2000, 06:08 PM
tron's Avatar
tron tron is offline
SwollenMember
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Location: the master control
Posts: 264 tron User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 14 m 57 sec
Reputation Power: 13
freebsd...you have solved my woes once again. Thanks!

Reply With Quote
  #8  
Old August 7th, 2000, 10:06 AM
JonLed JonLed is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2000
Location: Indiana
Posts: 614 JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level) 
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?

Reply With Quote
  #9  
Old August 7th, 2000, 10:40 AM
goBoating goBoating is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2000
Location: Charleston, SC, USA
Posts: 10 goBoating User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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...

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > yet another regex question

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap