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:
again on managing string ..

 



graziano68
Novice

Nov 1, 2002, 1:08 AM

Post #1 of 10 (1283 views)
again on managing string .. Can't Post

Hello,

I have another problem managing string ..

I have

$email = "test@foo.bar";

and I need to print only "test" (excluding @foo.bar ) .

Any idea to do that ? Thank you






thodi
stranger

Nov 1, 2002, 9:55 AM

Post #2 of 10 (1274 views)
Re: [graziano68] again on managing string .. [In reply to] Can't Post

You also seem to have a problem figuring out things for yourself. Try to think of a solution which matches any character up to the first '@' in a string. Try using index(), or rindex(), or m//, ...


graziano68
Novice

Nov 1, 2002, 12:56 PM

Post #3 of 10 (1272 views)
Re: [thodi] again on managing string .. [In reply to] Can't Post


In Reply To
You also seem to have a problem figuring out things for yourself. Try to think of a solution which matches any character up to the first '@' in a string. Try using index(), or rindex(), or m//, ...



I usually use php (I love it Tongue), but now I need perl to modify an existent perl script ..
I am trying to adapt your last suggestion on this problem (regarding hostname), however I was not succesfull (this time I need to esclude text after @ and not before...) . I spent a lot of hours to fix this stupid problem reading about string on perl sites , but I cannot find a solution ... why perl (especially working on string) is so complicated ?

May you help me more , I don't want buy a perl book only to solve
a string problem ..Unsure

Bye
Graz


thodi
stranger

Nov 1, 2002, 1:10 PM

Post #4 of 10 (1271 views)
Re: [graziano68] again on managing string .. [In reply to] Can't Post

Working with strings in Perl is complicated? Not really. One of many ways: First find the '@', then what you want is everything before that.

Code
# Assumes you have not messed around with $[ 
$index = index $email, '@';
if ($index < 0) {
die "'@' not found :-(";
} else {
$what_i_want = substr $email, 0, $index;
}

What would your php version look like?


graziano68
Novice

Nov 1, 2002, 2:31 PM

Post #5 of 10 (1272 views)
Re: [thodi] again on managing string .. [In reply to] Can't Post


In Reply To
Working with strings in Perl is complicated? Not really. One of many ways: First find the '@', then what you want is everything before that.

Code
 # Assumes you have not messed around with $[  
$index = index $email, '@';
if ($index < 0) {
die "'@' not found :-(";
} else {
$what_i_want = substr $email, 0, $index;
}

What would your php version look like?



A variety of methods are open with php.

For example more easy is using list


$email ="test@foo.bar";
list($user,$domain) = explode("@",$email);

$user will now be "test"

However ... thank you for your help , but it doesn't work for me , $what_i_want returns nothing , have you tried the lines above ?



thanks
Graz


graziano68
Novice

Nov 1, 2002, 2:37 PM

Post #6 of 10 (1269 views)
Re: [graziano68] again on managing string .. [In reply to] Can't Post

No , your lines works perfectly , thank you !!!

p.s. may you (or anyone) suggest me best link to learn perl and to find code examples ? Thanks


thodi
stranger

Nov 2, 2002, 12:15 AM

Post #7 of 10 (1264 views)
Re: [graziano68] again on managing string .. [In reply to] Can't Post


In Reply To
A variety of methods are open with php.

As in Perl ...

In Reply To
For example more easy is using list

$email ="test@foo.bar";
list($user,$domain) = explode("@",$email);

$user will now be "test"

There's something like that in Perl, too:

Code
my ($user, $domain) = split /@/, $email;

Or what about this one, with a regular expression (like your previous problem:

Code
$email =~ m/([^@]+)@(.*))/; 
my ($user, $domain) = ($1, $2);

... and so on. Perl has wonderful functions to work with strings. A good way to learn these pactical examples is the "Perl Cookbook" from O'Reilly.


graziano68
Novice

Nov 2, 2002, 1:54 AM

Post #8 of 10 (1264 views)
Re: [graziano68] again on managing string .. [In reply to] Can't Post

Is there any site similar of php.net for perl ?

I want mean .. A site where you can search a perl function.. ,.. you may read the documentation ... and at the same time you can read some code example too (such as on php.net web site) .


thodi
stranger

Nov 2, 2002, 2:17 AM

Post #9 of 10 (1264 views)
Re: [graziano68] again on managing string .. [In reply to] Can't Post

None that I know of like www.php.net (I really like that they have examples, too), sorry. But there's http://www.perldoc.com, where you can at least look up functions and the like (but all you find there is also available with the 'perldoc' program that comes with your installation of Perl). Then there's http://search.cpan.org to search for modules.


podmaster
Novice

Nov 2, 2002, 3:52 AM

Post #10 of 10 (1262 views)
Re: [graziano68] again on managing string .. [In reply to] Can't Post

all the perl documentation that
comes with perl is more than full of examples

perl.com has tons of articles as well

then there is tons of newsgroups and mailing lists
http://lists.perl.org/

 
 


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

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