Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I search a line of text for known string 1

Status
Not open for further replies.

fortytwo

Technical User
Apr 18, 2000
206
GB
Hi<br><br>I have a variable ($line) that contains multiple instances of the string &quot;web/&quot; followed by an unknown 6 character string.&nbsp;&nbsp;Is there any way I can search for every instance of web/ then readthe next 6 characters into another variable (for inclusion in a file/display?)<br><br>Thanks<br>Will
 
Try this - <br><br>$' returns everything <i>after</i> a matched pattern.<br>$1 returns something remembered with brackets in a pattern<br><br><b><FONT FACE=monospace><br>&nbsp;&nbsp;&nbsp;&nbsp;$_ = $line;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;while(/web\/(......)/){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$charstr=$1; # this is the string you were looking for<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$_=$'; # chop everything off $_ before the current match<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# do something with $charstr<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br></b></font><br><br>Mike<br> <p>Mike Lacey<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
Hi Mike<br><br>That was really great.&nbsp;&nbsp;It was exactly what I was looking for.&nbsp;&nbsp;any suggestions on learning resources?<br><br>Thanks<br>Will <p>fortytwo<br><a href=mailto:will@hellacool.co.uk>will@hellacool.co.uk</a><br><a href= test site</a><br>
 
Will,<br><br>Glad it worked.<br><br>The place to start for perl resources is <A HREF=" TARGET="_new"> and my main reference book is &quot;Programming Perl&quot; from O'Reilly (ISBN 1-56592-149-6), I wouldn't be without this book - and it has a nice picture of a camel on the front as well.<br><br>Whilst I think about it - that first line in my example:<br><br><FONT FACE=monospace><b>$_ = $line</font></b><br><br>Was just to make my example code clearer as most (a lot any) perl things work on <FONT FACE=monospace><b>$_</font></b> by default.<br><br>Without that line the pattern matching would be written<br><br><FONT FACE=monospace><b>while($line =~ /web\/(......)/){</font></b><br><br>Regards,<br><br>Mike.<br> <p>Mike Lacey<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
$_ is the variable that stores the current working variable, if that maeks sense... in a for loop, you could say:<br><br><FONT FACE=monospace><br>foreach (@array) {<br>&nbsp;&nbsp;print $_;<br>}<br></font><br>... and it would print every element in the array
 
<FONT FACE=monospace><b>$_</font></b> is a sort of catch-all default.<br><br>Lot's of things work on <FONT FACE=monospace><b>$_</font></b> unless you say otherwise. <FONT FACE=monospace><b>print;</font></b> for example is the same as saying <FONT FACE=monospace><b>print $_;</font></b>, <FONT FACE=monospace><b>chomp;</font></b> the same as <FONT FACE=monospace><b>chomp $_;</font></b><br>and <FONT FACE=monospace><b>$_ ~= /somestring/;</font></b> does the same as <FONT FACE=monospace><b>/somestring/;</font></b><br><br>I tend to use the defaults without specifying them - but I probably shouldn't. When others are trying to read your code it can be a problem. Perl code that uses defaults looks incredibly cryptic to a beginner.<br><br>Mike<br> <p>Mike Lacey<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
So basically, if a variable is not specified the data goes into $_&nbsp;&nbsp;as in:<br><br>foreach (@array) {<br>&nbsp;&nbsp;print $_;<br>}<br><br>which does the same as:<br><br>foreach $section (@array) {<br>&nbsp;&nbsp;print $section;<br>}<br><br>so you do not have to specify a variable, the variable is the sort of default variable.&nbsp;&nbsp;Or have i missed the mark (likely)<br><br>Will<br> <p>fortytwo<br><a href=mailto:will@hellacool.co.uk>will@hellacool.co.uk</a><br><a href= test site</a><br>
 
That's right, same thing happens here:<br><br><FONT FACE=monospace><b><br>open(F, &quot;an_input_file&quot;) ¦¦ die;<br>while(&lt;F&gt;){<br>&nbsp;&nbsp;&nbsp;&nbsp;print if /text i am searching for/o;<br>}<br></font></b><br><br>This bit of code prints all lines in the file an_input_file that contain the text &quot;text i am searching for&quot;.<br><br>It uses the default $_ thingy twice on the same line, once as the default argument to <FONT FACE=monospace><b>print</font></b> and again as the default variable to search (as in <FONT FACE=monospace><b>/text i am searching for/</font></b>)<br> <p>Mike<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top