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: Intermediate:
Best way to separate string?

 



RandyL712
Novice

Mar 1, 2002, 12:34 PM

Post #1 of 13 (2018 views)
     Best way to separate string?  

I have a string that looks like this:


Code
<h2>Values Follow</h2>LOGIN=testgate<BR>AMOUNT=147.95<BR>TYPE=<BR>CARDTYPE=VISA<BR>CARDNUM=41XXXXXXXXXX1111<BR>


I need to grab all of the attributes and their values out of it. I don't need to worry about escape/unescape as the values are fine as is.


mire
Novice

Mar 1, 2002, 8:02 PM

Post #2 of 13 (2010 views)
     Re: [RandyL712] Best way to separate string? [In reply to]  

Do a split, first on the ampersand sign and then on the equals sign.


_______________________________________________

Best regards,

Miroslav Madzarevic, Senior Perl Programmer
perl@modperldev.com
Mod Perl Development  - http://www.modperldev.com
Telephone: +381 64 1193 501
ICQ: 15880893
jamph


freddo
User

Mar 2, 2002, 4:28 AM

Post #3 of 13 (2007 views)
     Re: [RandyL712] Best way to separate string? [In reply to]  

Hello Randy,

Here it is my try at it:[perl]$verify = '<h2>Values Follow</h2>LOGIN=testgate<BR>AMOUNT=147.95<BR>TYPE=<BR>CARDTYPE=VISA<BR>CARDNUM=41XXXXXXXXXX1111<BR>';

@retval = qw/login amount type cardtype cardnum/;
$regex = ".*?";
map { $regex .= "$_=(.*?)<BR>" } @retval;
print "Regex is $regex\n";

print "Array:\n";
@values = $verify =~ /$regex/i; # get result in an array
print join "\n", @values;

print "\nHash:\n";
@value{@retval} = $verify =~ /$regex/i; # get result in a hash
print "CardNum = ", $value{cardnum}, "\n"; # print cardnum[/perl]
Now, keep in mind that:
1) the regex is case insensitive (dunno how this could be BAD)
2) Everything is accepted as a value (.*?) and you should better set valid ranges like [:alnum:] or [\.0-9]...

Anyway, i hope you get what you want, if the hash thing seems a little strange, see Jasmine comments here (btw, thanks jasmine, i didnt knew that really nice feature of perl, i guess i'll get smaller .pl files now then Tongue).

Freddo
;---


dave
Novice

Mar 14, 2002, 12:51 AM

Post #4 of 13 (1990 views)
     Re: [RandyL712] Best way to separate string? [In reply to]  

I know I'm a bit late with this, but here's mine...


Code
  

my %nv = my @nv = $verify =~ />([^>]+?)=(.*?)</g;


print "$_\n" for @nv;
print "\n";
print "$_ = $nv{$_}\n" for keys %nv;


dave


Paul
Enthusiast

Mar 14, 2002, 4:14 AM

Post #5 of 13 (1987 views)
     Re: [RandyL712] Best way to separate string? [In reply to]  

The string you gave is inconsistent. Are they typos?

eg... <BR>TYPE=<BR>

Anyway:


Code
print "$1 = $2\n" while $verify =~ m/(\w+)=(\w+)/sg;



(This post was edited by RedRum on Mar 14, 2002, 4:18 AM)


dave
Novice

Mar 14, 2002, 10:22 AM

Post #6 of 13 (1973 views)
     Re: [RedRum] Best way to separate string? [In reply to]  


Quote
The string you gave is inconsistent. Are they typos?

eg... <BR>TYPE=<BR>


I think you'll find that's a name without a value, not an inconsistency. So your code will
not catch the TYPE name.


Quote
m/(\w+)=(\w+)/sg;


And if you're already treating the data as a string, there's no need for the /s modifier.

dave


Paul
Enthusiast

Mar 14, 2002, 10:36 AM

Post #7 of 13 (1970 views)
     Re: [RedRum] Best way to separate string? [In reply to]  

Simple enough change to fix that:


Code
print "$1=$2\n" while $verify =~ m/(\w+)=(.*?)</g;


I believe it is shorter than yours too... Tongue...no need to initialize an array and a hash.


(This post was edited by RedRum on Mar 14, 2002, 10:39 AM)


dave
Novice

Mar 14, 2002, 2:07 PM

Post #8 of 13 (1961 views)
     Re: [RedRum] Best way to separate string? [In reply to]  

It is shorter. Good one.
But you could lose that loop and second reference...


Code
 $verify =~ s/(\w+=.*?)</print"$1\n"/eg;



Your turn!
dave


Paul
Enthusiast

Mar 15, 2002, 4:13 AM

Post #9 of 13 (1955 views)
     Re: [dave] Best way to separate string? [In reply to]  

Why didnt you give that as your first answer :)

Its always easier when youve had time to think Wink


Quote
It is shorter. Good one.
But you could lose that loop and second reference...
Your turn!


Haha I like the way you think you are unbeatable.


Tongue

$verify =~ s/>(\S+?)</print"$1\n"/eg;

I believe that beats you by 2 characters..."your turn"


(This post was edited by RedRum on Mar 15, 2002, 7:17 AM)


dave
Novice

Mar 15, 2002, 2:30 PM

Post #10 of 13 (1929 views)
     Re: [RedRum] Best way to separate string? [In reply to]  

You just keep coming back for more don't you?!


Code
 $verify =~ s/>(\S+?)</print$1/eg;




Quote
Why didnt you give that as your first answer :)




I could ask the same of you.



Quote
Its always easier when youve had time to think




Yes. And it's even easier when you've been shown how. And at least all of
my attempts actually worked. Actually, I thought there might be a shorter way, and you found one.
Congratulations!



Quote
Haha I like the way you think you are unbeatable.




*Nobody* is, or ever could be unbeatable. That's a fact. But I don't think there's anything wrong
with striving for such status even though we know we can never attain it. It's called healthy competition, and we learn a lot from it.


By the way, you need to lose 5 characters or else, in this thread at least, I am unbeatable!!...with a
little help from your good self I hasten to add!

dave


Paul
Enthusiast

Mar 16, 2002, 1:36 AM

Post #11 of 13 (1920 views)
     Re: [dave] Best way to separate string? [In reply to]  

>>Yes. And it's even easier when you've been shown how.<<

Thats called "evolution" .....thats how new code is developed by people "evolving" existing code. How do you think Netscape came about Cool

>>And at least all of my attempts actually worked. <<

You are clearly a perl guru and I am your eternal slave Laugh

>>By the way, you need to lose 5 characters or else, in this thread at least, I am unbeatable!!...with a
little help from your good self I hasten to add!
<<

Sorry you've lost me. If you mean you've knocked 5 characters off the code then why not show us what you have?


(This post was edited by RedRum on Mar 16, 2002, 1:50 AM)


dave
Novice

Mar 16, 2002, 5:26 AM

Post #12 of 13 (1902 views)
     Re: [RedRum] Best way to separate string? [In reply to]  

It seems to be a bad habit you have; not reading the posts you're replying to properly. The code is at the top of my last post. It's valid because it pulls out all the attributes and values, which is what RandyL712 originally asked for.


Quote
Thats called "evolution" .....thats how new code is developed by people "evolving" existing code. How do you think Netscape came about



I agree. Would you agree that thinking is also a part of this evolution? My point is that you tried to rubbish me for taking time to think.


Quote
You are clearly a perl guru and I am your eternal slave



You know it!! Mush! Mush!


I'de just like to say that it was you who turned this discussion into some kind of competition by childishly gloating every time you came up with a shorter version. You seem like the type of person who can hand it out, but can't take it. You remind of old Mrs. Washerwoman, busy-bodying around, picking the nits out of other peoples code. Sometimes I've even seen you stipulating your preferences on perfectly legitimate code. Now that's arrogant.

If you manage to come up with a shorter version again, then congratulations in advance! I really have no more to time to spend here.

dave


Paul
Enthusiast

Mar 16, 2002, 6:48 AM

Post #13 of 13 (1897 views)
     Re: [dave] Best way to separate string? [In reply to]  

You need to learn what a joke is my friend.....I shortened your code and made a comment in jest.

You don't have to get personal.

Edit: btw i think you'll find the code "rubbishing" started in post 6 Crazy


(This post was edited by RedRum on Mar 16, 2002, 7:21 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