CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Getting values in the middle of the line

 



Zisu
Novice

Jul 17, 2002, 11:13 PM

Post #1 of 14 (2300 views)
Getting values in the middle of the line Can't Post

The problem: I have directory names, that I should use to make html documentations (a lot of dirs).

All dirs have similar construct: MSB-BLA_BLA_BLA-at-DATE_TIME-set(id)_(value)_(index) where the one marked in (id) can be 0 or 5 or 2U and other two () are numbers (\D+).

I want to get the first one to a variable and two last values as numbers (so i can use them as index in arrays) into variables, how do I manage this?
- Just another beginner -


Jean
User


Jul 18, 2002, 12:14 AM

Post #2 of 14 (2298 views)
Re: [Zisu] Getting values in the middle of the line [In reply to] Can't Post

Try this:

[perl]my ($res1, $idx1, $idx2);
($res1, $idx1, $idx2) = $str =~ /MSB-.+?-at-.+?-set(\([025][Uu]?\))_(\(\d+\))_(\(\d+\))/;
print "$res1 -- $idx1 -- $idx2\n";
[/perl]


Jean Spector
SQA Engineer @ Exanet
jean.spector@softhome.net


There are only 10 types of people in the world -
Those who understand binary, and those who don't.


(This post was edited by Jean on Jul 18, 2002, 12:15 AM)


Paul
Enthusiast

Jul 22, 2002, 3:31 AM

Post #3 of 14 (2286 views)
Re: [Jean] Getting values in the middle of the line [In reply to] Can't Post

You could change .+? to \w+ which would be a little nicer and more accurate.


(This post was edited by RedRum on Jul 22, 2002, 3:32 AM)


Jean
User


Jul 22, 2002, 3:43 AM

Post #4 of 14 (2281 views)
Re: [RedRum] Getting values in the middle of the line [In reply to] Can't Post

It depends on the contents of the BLA_BLA Smile
Dir names could include something other than \w+...


Jean Spector
SQA Engineer @ Exanet
jean.spector@softhome.net


There are only 10 types of people in the world -
Those who understand binary, and those who don't.


(This post was edited by Jean on Jul 22, 2002, 3:44 AM)


Paul
Enthusiast

Jul 22, 2002, 3:58 AM

Post #5 of 14 (2278 views)
Re: [Jean] Getting values in the middle of the line [In reply to] Can't Post

Oops yeah sorry, I took BLAH_BLAH and DATE_TIME as literal Blush


Jean
User


Jul 22, 2002, 4:07 AM

Post #6 of 14 (2276 views)
Re: [RedRum] Getting values in the middle of the line [In reply to] Can't Post

I guess it's all right - IMHO for thousands of helpful posts you can miss something that wasn't very obvious Wink Wink Wink


Jean Spector
SQA Engineer @ Exanet
jean.spector@softhome.net


There are only 10 types of people in the world -
Those who understand binary, and those who don't.


Zisu
Novice

Jul 23, 2002, 7:57 AM

Post #7 of 14 (2269 views)
Re: [Jean] Getting values in the middle of the line [In reply to] Can't Post

Got it to work. Thank you for the help Cool
- Just another beginner -


Jean
User


Jul 23, 2002, 8:12 AM

Post #8 of 14 (2268 views)
Re: [Zisu] Getting values in the middle of the line [In reply to] Can't Post

Anytime Cool


Jean Spector
SQA Engineer @ Exanet
jean.spector@softhome.net


There are only 10 types of people in the world -
Those who understand binary, and those who don't.


Zisu
Novice

Jul 25, 2002, 2:22 AM

Post #9 of 14 (2262 views)
Re: [Jean] Getting values in the middle of the line [In reply to] Can't Post

I want to change line:

<A HREF="./link.html" TARGET=_top>Thursday, July 11, 2002, 09:27:57</A>

and +.? doesnt work as I expected.

$header = $linea =~ s/>.+?</>Something else</;

How would you change the line to be:

<A HREF="./link.html" TARGET=_top>Something else</A>

?
- Just another beginner -


Jean
User


Jul 25, 2002, 3:08 AM

Post #10 of 14 (2260 views)
Re: [Zisu] Getting values in the middle of the line [In reply to] Can't Post

Actually you code works fine Smile - you've just added redundant variable...

$linea =~ s/>.+?</>Something else</; should work fine (with updated contents stored in the $linea)

Moreover - in your case you can even omit the ? in the regular expression:
$linea =~ s/>.+</>Something else</;
The ? is importany only if you have more than one < in your string...


Jean Spector
SQA Engineer @ Exanet
jean.spector@softhome.net


There are only 10 types of people in the world -
Those who understand binary, and those who don't.


(This post was edited by Jean on Jul 25, 2002, 3:12 AM)


Paul
Enthusiast

Jul 25, 2002, 3:11 AM

Post #11 of 14 (2257 views)
Re: [Jean] Getting values in the middle of the line [In reply to] Can't Post

>>
.+?
<<

I believe:

[^<]+

...would be quicker as it doesn't have to read ahead.


Jean
User


Jul 25, 2002, 3:18 AM

Post #12 of 14 (2255 views)
Re: [RedRum] Getting values in the middle of the line [In reply to] Can't Post

As long as performance is an issue - and it usually is Smile, the [^<] is definitely a better choice here Cool


Jean Spector
SQA Engineer @ Exanet
jean.spector@softhome.net


There are only 10 types of people in the world -
Those who understand binary, and those who don't.


Zisu
Novice

Jul 25, 2002, 3:51 AM

Post #13 of 14 (2252 views)
Re: [Jean] Getting values in the middle of the line [In reply to] Can't Post

Im working outside from web. I simulate (at work) with a tool that can generate crappy webpages (tens of folders with tens of files in them per simulation). Im working on a script that would correct them. So the speed isnt the essence. Nevertheless:

I dont understand the use of [^<]+

Can someone put it to right place of the code and explain a bit?
- Just another beginner -


Jean
User


Jul 25, 2002, 4:05 AM

Post #14 of 14 (2249 views)
Re: [Zisu] Getting values in the middle of the line [In reply to] Can't Post

Additiona warning - not a professional explanation Smile

[ ] - is a block that may include any number of characters and may be substituted for one character in the match
e.g. as /abc.efg/ matches 'abcdefg', so /abc[dV]efg/ matches 'abcdefg' and 'abcVefg'

^ - in case it appears as the first character inside [] means the opposite
e.g. /abc[^V]efg/ matches 'abcdefg' and any other 'abc.efg' except 'abcVefg'

Thus, [^<]+ will match all the charactes until it meets the <...

Hope it's not too misleading Unsure

In general, both solutions will work for you - it's just better to know your options Wink


Jean Spector
SQA Engineer @ Exanet
jean.spector@softhome.net


There are only 10 types of people in the world -
Those who understand binary, and those who don't.


(This post was edited by Jean on Jul 25, 2002, 4:06 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives