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:
Line Delimiting Question

 



bclark8923
Novice

Jun 29, 2009, 9:01 AM

Post #1 of 14 (1807 views)
Line Delimiting Question Can't Post

If i have the string:

"Brian D Clark"

and wanted to have vars

$first = "Brian"
$mid = "D"
$last = "Clark"

how would i go about doing that dynamically?


1arryb
User

Jun 29, 2009, 9:43 AM

Post #2 of 14 (1802 views)
Re: [bclark8923] Line Delimiting Question [In reply to] Can't Post

Hi bclark,


Code
#!/usr/bin/perl 

use strict;
use warnings;

my $fullName = "Brian D Clark";
my ($first, $mid, $last) = split(/ /, $fullName); # Note: This is a naive name parse.
print "first = $first, mid = $mid, last = $last\n";


Cheers,

Larry


ichi
User

Jun 29, 2009, 6:26 PM

Post #3 of 14 (1796 views)
Re: [bclark8923] Line Delimiting Question [In reply to] Can't Post


Code
$string = "Brian D Clark" ; 
@s = split /\s+/,$string;

you don't have to declare separate variables like $first,$second etc. Just use array indexing to get your values. eg $s[0] etc..


vikas.deep
User

Jun 29, 2009, 9:56 PM

Post #4 of 14 (1791 views)
Re: [ichi] Line Delimiting Question [In reply to] Can't Post

Already two persons have replied here, so there is really no need of anything extra here but I am surprised that the strategy to tackle this problem that first struck my mind was not used by any of the above mentioned two responses. So I am posting my solution here, I would consider it my good fortune if I receive a criticism for this (my) approach

Code
#! usr/bin/perl 
use strict;
use warnings;
my $string = "Brian D Clark";
if ($string =~ /^(.+)\s(.+)\s(.+)$/)
{print $1,"\n",$2,"\n",$3}
#The output is
#[root@localhost Documents]# perl hell.pl
#Brian
#D
#Clark[root@localhost Documents]#

-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"


vikas.deep
User

Jun 29, 2009, 10:05 PM

Post #5 of 14 (1790 views)
Re: [bclark8923] Line Delimiting Question [In reply to] Can't Post


Quote
how would i go about doing that dynamically?

I think this is the most important line.
What do you mean by dynamically Do you mean that name is to be initialised at run time and you will ask the user to enter his name. You mean (to me) that you are not sure about the name and it can be anything but for sure it will always have a first, middle and a last name; separated by white spaces.
So if that is the case you should use $1...$x and not initialize any variable.

Quote
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"


ichi
User

Jun 29, 2009, 10:19 PM

Post #6 of 14 (1789 views)
Re: [vikas.deep] Line Delimiting Question [In reply to] Can't Post


In Reply To
Already two persons have replied here, so there is really no need of anything extra here but I am surprised that the strategy to tackle this problem that first struck my mind was not used by any of the above mentioned two responses. So I am posting my solution here, I would consider it my good fortune if I receive a criticism for this (my) approach

Code
#! usr/bin/perl 
use strict;
use warnings;
my $string = "Brian D Clark";
if ($string =~ /^(.+)\s(.+)\s(.+)$/)
{print $1,"\n",$2,"\n",$3}
#The output is
#[root@localhost Documents]# perl hell.pl
#Brian
#D
#Clark[root@localhost Documents]#



the issue with your approach, it only works with that particular string. What if the string is something else . eg "one two three four five". then your regex will not work. Same issue as the solution to split on spaces and assign to 3 variables. Its best to split to an array, and if OP needs to get the relevant items, just use array indexing.


KevinR
Veteran


Jun 29, 2009, 10:51 PM

Post #7 of 14 (1785 views)
Re: [ichi] Line Delimiting Question [In reply to] Can't Post


Quote
Its best to split to an array, and if OP needs to get the relevant items, just use array indexing.


That is your opinion. I beg to differ. I would use the code Larry posted if it is three values being stored in three scalars, to me that seems like the "best" way for that particular requirement.
-------------------------------------------------


ichi
User

Jun 30, 2009, 2:01 AM

Post #8 of 14 (1780 views)
Re: [KevinR] Line Delimiting Question [In reply to] Can't Post


In Reply To

Quote
Its best to split to an array, and if OP needs to get the relevant items, just use array indexing.


That is your opinion. I beg to differ. I would use the code Larry posted if it is three values being stored in three scalars, to me that seems like the "best" way for that particular requirement.


scalars and arrays don't differ much in the way they are used. They both are memory locations. I foresee OP will have to parse different types of names in future and not just like the one in the sample. That's why i suggest by splitting to array instead of defining the number of scalars will be better. That's way, code won't break in future when names are of different format.


vikas.deep
User

Jun 30, 2009, 2:39 AM

Post #9 of 14 (1778 views)
Re: [KevinR] Line Delimiting Question [In reply to] Can't Post

Very fortunately some people have a penchant/ inclination to open every odd can of worms that happen to be on this planet. It greatly helps to get rid of one's ignorance.


Quote
I would use the code Larry posted if it is three values being stored in three scalars, to me that seems like the "best" way for that particular requirement.

Fair enough but what is the reason to initialize three separate variables $first, $mid and $last; especially when we can use special variables $1, $2 and $3 ----.

Further, the code posted by ichi appears to be more general than Larry's code and hence you should have preferred it; if you say you prefer Larry's code sure you must be having some strong reasons for that What are they?

Quote
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"

(This post was edited by vikas.deep on Jun 30, 2009, 2:44 AM)


KevinR
Veteran


Jun 30, 2009, 11:08 AM

Post #10 of 14 (1770 views)
Re: [ichi] Line Delimiting Question [In reply to] Can't Post


In Reply To

In Reply To

Quote
Its best to split to an array, and if OP needs to get the relevant items, just use array indexing.


That is your opinion. I beg to differ. I would use the code Larry posted if it is three values being stored in three scalars, to me that seems like the "best" way for that particular requirement.


scalars and arrays don't differ much in the way they are used. They both are memory locations. I foresee OP will have to parse different types of names in future and not just like the one in the sample. That's why i suggest by splitting to array instead of defining the number of scalars will be better. That's way, code won't break in future when names are of different format.


ichi,

You can do it that way if you prefer, but most perl coders prefer using scalar names that make sense, like $name or $address instead of $array[1] or $array[2]. If the list was big, instead of just three scalars, then splitting into an array could be a better option.

Its good to write code with the future in mind. Your suggestion of returning the results of split to an array is an example of that. That is a good suggestion when you never know how many results might be returned to a list and you need to get them all and then loop through them or search through them or similar.

But this question the OP asked is very common and quite specific, and for that question and those requirements I would return the results back to a list of three appropriately named scalars.
-------------------------------------------------


KevinR
Veteran


Jun 30, 2009, 11:19 AM

Post #11 of 14 (1769 views)
Re: [vikas.deep] Line Delimiting Question [In reply to] Can't Post


In Reply To
Very fortunately some people have a penchant/ inclination to open every odd can of worms that happen to be on this planet. It greatly helps to get rid of one's ignorance.


Quote
I would use the code Larry posted if it is three values being stored in three scalars, to me that seems like the "best" way for that particular requirement.

Fair enough but what is the reason to initialize three separate variables $first, $mid and $last; especially when we can use special variables $1, $2 and $3 ----.

Further, the code posted by ichi appears to be more general than Larry's code and hence you should have preferred it; if you say you prefer Larry's code sure you must be having some strong reasons for that What are they?

Quote


Regular expressions or used to look for patterns. Patterns could be something like:

12234vvvXXXX
23jjjjjjTTTTTTT

several digits followed by several lower-case characters then several upper-case characters. But the pattern could vary although it sticks to that basic formula. For that you would use a regexp:

/(\d+)([a-z]+)([A-Z]+)/

but in this situation were are not looking for patterns, we are splitting a string/line into tokens (a list or array) using an established delimiter. For that you use the split() function. If the fields are fixed-length you would want to use unpack() which is more efficient than split().

I appreciate that you want to understand the "why" of things. But it is also very time consuming to try and explain all the reason why some code is better than other code, and sometimes I frankly don't know why. I often defer to people that I get information from, like Damien Conway, or Brian Defoy, two very respected perl people, and if they say one way is better than another I take their word for it until its proved otherwise.
-------------------------------------------------


(This post was edited by KevinR on Jun 30, 2009, 11:23 AM)


ichi
User

Jun 30, 2009, 5:37 PM

Post #12 of 14 (1759 views)
Re: [KevinR] Line Delimiting Question [In reply to] Can't Post

kevin,

well, i am not going to agree or disagree any further, the final decision to use which method is not up to us to decide, so i will just leave it as that.


vikas.deep
User

Jun 30, 2009, 9:33 PM

Post #13 of 14 (1754 views)
Re: [KevinR] Line Delimiting Question [In reply to] Can't Post

Thanks for a detailed reply. This is "the" "detailed" and accurate criticism (of my code wherein i used a regular expression ) that I was looking for since yesterday. Now it is clear and doubts have been rested.

Now of course all of it seems very obvious. Also, (Suddenly) I remember my teachers always stressed "Use Meaningful names for variables".

Now, It all looks very simple, as if it was just waiting for a detailed response from your end. Thank you

Quote
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"

(This post was edited by vikas.deep on Jun 30, 2009, 9:36 PM)


KevinR
Veteran


Jun 30, 2009, 10:46 PM

Post #14 of 14 (1749 views)
Re: [ichi] Line Delimiting Question [In reply to] Can't Post


In Reply To
kevin,

well, i am not going to agree or disagree any further, the final decision to use which method is not up to us to decide, so i will just leave it as that.


Ask on www.stackoverflow.com if you want more opinions on anything. There are a number of really experienced programmers replying to questions.

http://stackoverflow.com/questions/tagged/perl

stackoverflow is not a forum, its a rather unique format they have. You might find it interesting.
-------------------------------------------------

 
 


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

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