Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > split function syntax

Reply
Thread Tools

split function syntax

 
 
Mostafa
Guest
Posts: n/a
 
      12-01-2004
open(PH,"customers.txt") or die "Cannot open customers.txt: $!\n";
while(<PH>) {
chomp;
($number, $email) = ( split(/\s+/, $_) )[1,2];

$Phone{$number} = $_;
$Email{$email} = $_;
}
close(PH);
************************************************** *****************
i couldn't find what does this expression means in split function
[1,2]

************************************************** ********************
i am also including the customers.txt file below
************************************************** ******************
Smith,John (24-555-9430

Hunter,Apryl (810)-555-3029

Stewart,Pat (405)-555-8710

Ching,Iris (305)-555-0919

Doe,John (212)-555-0912

Jones,Tom (312)-555-3321

Smith,John (607)-555-0023

Crosby,Dave (405)-555-1516

Johns,Pam (313)-555-6790

Jeter,Linda (810)-555-8761

Garland,Judy (305)-555-1231
************************************************** *********************
i guess [1,2 ]somehow saves phone and email column to $number and
$email , nnow if i want to retrive name from the .txt file , then
should i do like below---
************************************************** ***********************
open(PH,"customers.txt") or die "Cannot open customers.txt: $!\n";
while(<PH>) {
chomp;
($name, $number, $email) = ( split(/\s+/, $_) )[0,1,2];
$Name{name} = $_;
$Phone{$number} = $_;
$Email{$email} = $_;
}
close(PH);
************************************************** *********************
i did but it's not working
************************************************** ******************
any clue?????????
 
Reply With Quote
 
 
 
 
Sherm Pendley
Guest
Posts: n/a
 
      12-01-2004
Mostafa wrote:

> ($number, $email) = ( split(/\s+/, $_) )[1,2];
>
> ************************************************** *****************
> i couldn't find what does this expression means in split function
> [1,2]


It's called a "slice". split() returns a list, and [1,2] is used to
return a subset of that list. Specifically, the elements at index 1 and
2, which are the phone and email.

Have a look at 'perldoc perldata' - there's a section titled "Slices".

> ************************************************** ********************
> i am also including the customers.txt file below
> ************************************************** ******************


Amusing names & email addresses, by the way.

> ************************************************** *********************
> i guess [1,2 ]somehow saves phone and email column to $number and
> $email , nnow if i want to retrive name from the .txt file , then
> should i do like below---
> ************************************************** ***********************
> open(PH,"customers.txt") or die "Cannot open customers.txt: $!\n";
> while(<PH>) {
> chomp;
> ($name, $number, $email) = ( split(/\s+/, $_) )[0,1,2];
> $Name{name} = $_;
> $Phone{$number} = $_;
> $Email{$email} = $_;
> }
> close(PH);
> ************************************************** *********************
> i did but it's not working
> ************************************************** ******************


Define "not working". Since you want all three fields now, you could do
away with the slice entirely, and simply use:

($name, $number, $email) = split(/\s+/, $_);

You could simplify it further. split() defaults to splitting $_ if no
other variable is given, and defaults to splitting on white space if no
other pattern is given. So you could use:

($name, $number, $email) = split;

Finally, it's not directly related to your question, but you *really*
should be using strict and warnings, and declaring these variables
properly with "my" or "our" as appropriate. Have you seen the posting
guidelines that appear here frequently?

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      12-01-2004
(Mostafa) wrote in
news: om:

I don't see

use strict;
use warnings;

> open(PH,"customers.txt") or die "Cannot open customers.txt: $!\n";
> while(<PH>) {
> chomp;
> ($number, $email) = ( split(/\s+/, $_) )[1,2];


my ($number, $email) = ( split(/\s+/, $_) )[1,2];

>
> $Phone{$number} = $_;
> $Email{$email} = $_;
> }
> close(PH);


....

> i couldn't find what does this expression means in split function
> [1,2]


...

[1,2] is not an argument to the split function. The syntax is for selecting
array slices. For example:

my @ary = qw(a b c d);
print join "\n", @ary[1,2];

Now, in the code above, you are creating hash tables that allow you to look
up the full record by phone number and email. I am assuming that is what
you want but do look at the resulting data structures using Data:umper to
make sure that is indeed what you want.

use strict;
use warnings;

my %Phone;
my %Email;

while(<DATA>) {
chomp;
my ($number, $email) = ( split /\s+/, $_ )[1,2];
$Phone{$number} = $_;
$Email{$email} = $_;
}

use Data:umper;
print Dumper \%Phone, \%Email;

__DATA__
Hunter,Apryl (810)-555-3029
Stewart,Pat (405)-555-8710
Ching,Iris (305)-555-0919
__END__

C:\Home> ph
$VAR1 = {
'(405)-555-8710' => 'Stewart,Pat (405)-555-8710 ',
'(305)-555-0919' => 'Ching,Iris (305)-555-0919 ',
'(810)-555-3029' => 'Hunter,Apryl (810)-555-3029 '
};

$VAR2 = {
'' => 'Ching,Iris (305)-555-0919 ',
'' => 'Hunter,Apryl (810)-555-3029 ',
'' => 'Stewart,Pat (405)-555-8710
'
};


> i guess [1,2 ]somehow saves phone and email column to $number and
> $email , nnow if i want to retrive name from the .txt file , then
> should i do like below---


I still don't see:

use strict;
use warnings;

> open(PH,"customers.txt") or die "Cannot open customers.txt: $!\n";
> while(<PH>) {
> chomp;
> ($name, $number, $email) = ( split(/\s+/, $_) )[0,1,2];


my ($name, $number, $email) = ( split(/\s+/, $_) )[0,1,2];

Well, this time you don't really need to explicitly select the elements of
the returned array.

> $Name{name} = $_;
> $Phone{$number} = $_;
> $Email{$email} = $_;
> }
> close(PH);


....

> i did but it's not working


That is not a helpful description of the problem you are experiencing. have
you read the posting guidelines posted here regularly?

use strict;
use warnings;

my %Phone;
my %Email;
my %Name;

while(<DATA>) {
chomp;
my ($name, $number, $email) = split /\s+/, $_;
$Phone{$number} = $_;
$Email{$email} = $_;
$Name{$name} = $_;
}

use Data:umper;
print Dumper \%Phone, \%Email, \%Name;

__DATA__
Hunter,Apryl (810)-555-3029
Stewart,Pat (405)-555-8710
Ching,Iris (305)-555-0919
__END__


C:\Home\asu1> ph
$VAR1 = {
'(405)-555-8710' => 'Stewart,Pat (405)-555-8710 ',
'(305)-555-0919' => 'Ching,Iris (305)-555-0919 ',
'(810)-555-3029' => 'Hunter,Apryl (810)-555-3029 '
};
$VAR2 = {
'' => 'Ching,Iris (305)-555-0919 ',
'' => 'Hunter,Apryl (810)-555-3029 ',
'' => 'Stewart,Pat (405)-555-8710
'
};
$VAR3 = {
'Hunter,Apryl' => 'Hunter,Apryl (810)-555-3029 ',
'Ching,Iris' => 'Ching,Iris (305)-555-0919 ',
'Stewart,Pat' => 'Stewart,Pat (405)-555-8710 '
};

> any clue?????????


Depending on exactly what you are trying to accomplish, you might benefit
from using DBI with DBD::CSV.


--
A. Sinan Unur
d
(remove '.invalid' and reverse each component for email address)

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      12-01-2004
Mostafa <> wrote:

> ($number, $email) = ( split(/\s+/, $_) )[1,2];


> i couldn't find what does this expression means in split function
> [1,2]



See the "Slices" section in:

perldoc perldata



This should do the same thing and be easier to read:

($number, $email) = (split)[1,2];


> i guess [1,2 ]somehow saves phone and email column to $number and
> $email , nnow if i want to retrive name from the .txt file , then
> should i do like below---


> ($name, $number, $email) = ( split(/\s+/, $_) )[0,1,2];



If you want _all_ of the split fields, then there is no need
for any slicing at all:

($name, $number, $email) = split;


What if a user has a space character in their name or email address?

eg: Thorton,Billy Bob


> i did but it's not working


What did you want it to do?

What is it doing instead?

You must provide symptoms if you hope for a diagnosis.


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      12-01-2004
On 1 Dec 2004 03:59:23 GMT, "A. Sinan Unur" <>
wrote:

>> $Name{name} = $_;

^^^^
^^^^
>> $Phone{$number} = $_;
>> $Email{$email} = $_;
>> }
>> close(PH);

>
>...
>
>> i did but it's not working

>
>That is not a helpful description of the problem you are experiencing. have
>you read the posting guidelines posted here regularly?


I think he means "that" (^^^^). Seems reasonable...


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
Reply With Quote
 
Mostafa
Guest
Posts: n/a
 
      12-01-2004
"A. Sinan Unur" <> wrote in message news:<Xns95B1E9DD6A3D7asu1cornelledu@132.236.56.8> ...
> (Mostafa) wrote in
> news: om:
>
> I don't see
>
> use strict;
> use warnings;
>
> > open(PH,"customers.txt") or die "Cannot open customers.txt: $!\n";
> > while(<PH>) {
> > chomp;
> > ($number, $email) = ( split(/\s+/, $_) )[1,2];

>
> my ($number, $email) = ( split(/\s+/, $_) )[1,2];
>
> >
> > $Phone{$number} = $_;
> > $Email{$email} = $_;
> > }
> > close(PH);

>
> ...
>
> > i couldn't find what does this expression means in split function
> > [1,2]

>
> ..
>
> [1,2] is not an argument to the split function. The syntax is for selecting
> array slices. For example:
>
> my @ary = qw(a b c d);
> print join "\n", @ary[1,2];
>
> Now, in the code above, you are creating hash tables that allow you to look
> up the full record by phone number and email. I am assuming that is what
> you want but do look at the resulting data structures using Data:umper to
> make sure that is indeed what you want.
>
> use strict;
> use warnings;
>
> my %Phone;
> my %Email;
>
> while(<DATA>) {
> chomp;
> my ($number, $email) = ( split /\s+/, $_ )[1,2];
> $Phone{$number} = $_;
> $Email{$email} = $_;
> }
>
> use Data:umper;
> print Dumper \%Phone, \%Email;
>
> __DATA__
> Hunter,Apryl (810)-555-3029
> Stewart,Pat (405)-555-8710
> Ching,Iris (305)-555-0919
> __END__
>
> C:\Home> ph
> $VAR1 = {
> '(405)-555-8710' => 'Stewart,Pat (405)-555-8710 ',
> '(305)-555-0919' => 'Ching,Iris (305)-555-0919 ',
> '(810)-555-3029' => 'Hunter,Apryl (810)-555-3029 '
> };
>
> $VAR2 = {
> '' => 'Ching,Iris (305)-555-0919 ',
> '' => 'Hunter,Apryl (810)-555-3029 ',
> '' => 'Stewart,Pat (405)-555-8710
> '
> };
>
>
> > i guess [1,2 ]somehow saves phone and email column to $number and
> > $email , nnow if i want to retrive name from the .txt file , then
> > should i do like below---

>
> I still don't see:
>
> use strict;
> use warnings;
>
> > open(PH,"customers.txt") or die "Cannot open customers.txt: $!\n";
> > while(<PH>) {
> > chomp;
> > ($name, $number, $email) = ( split(/\s+/, $_) )[0,1,2];

>
> my ($name, $number, $email) = ( split(/\s+/, $_) )[0,1,2];
>
> Well, this time you don't really need to explicitly select the elements of
> the returned array.
>
> > $Name{name} = $_;
> > $Phone{$number} = $_;
> > $Email{$email} = $_;
> > }
> > close(PH);

>
> ...
>
> > i did but it's not working

>
> That is not a helpful description of the problem you are experiencing. have
> you read the posting guidelines posted here regularly?
>
> use strict;
> use warnings;
>
> my %Phone;
> my %Email;
> my %Name;
>
> while(<DATA>) {
> chomp;
> my ($name, $number, $email) = split /\s+/, $_;
> $Phone{$number} = $_;
> $Email{$email} = $_;
> $Name{$name} = $_;
> }
>
> use Data:umper;
> print Dumper \%Phone, \%Email, \%Name;
>
> __DATA__
> Hunter,Apryl (810)-555-3029
> Stewart,Pat (405)-555-8710
> Ching,Iris (305)-555-0919
> __END__
>
>
> C:\Home\asu1> ph
> $VAR1 = {
> '(405)-555-8710' => 'Stewart,Pat (405)-555-8710 ',
> '(305)-555-0919' => 'Ching,Iris (305)-555-0919 ',
> '(810)-555-3029' => 'Hunter,Apryl (810)-555-3029 '
> };
> $VAR2 = {
> '' => 'Ching,Iris (305)-555-0919 ',
> '' => 'Hunter,Apryl (810)-555-3029 ',
> '' => 'Stewart,Pat (405)-555-8710
> '
> };
> $VAR3 = {
> 'Hunter,Apryl' => 'Hunter,Apryl (810)-555-3029 ',
> 'Ching,Iris' => 'Ching,Iris (305)-555-0919 ',
> 'Stewart,Pat' => 'Stewart,Pat (405)-555-8710 '
> };
>
> > any clue?????????

>
> Depending on exactly what you are trying to accomplish, you might benefit
> from using DBI with DBD::CSV.


************************************************** ***********************
thanks a lot it's working;
but i am getting some warnings , how can i get rid of this warnings .
i also add following codes with my program ---
use strict;

my %Name;
my %Phone;
my %Email;
my $name;
my $number;
my $email;
my $address;


i am giving u the list of warnings for the line 17,18,19. i add those
lines right below the warnings.------

C:\mp>perl customer.pl
Use of uninitialized value in hash element at customer.pl line 17,
<PH> line 2.
Use of uninitialized value in hash element at customer.pl line 18,
<PH> line 2.
Use of uninitialized value in hash element at customer.pl line 19,
<PH> line 2.
Use of uninitialized value in hash element at customer.pl line 17,
<PH> line 4.
Use of uninitialized value in hash element at customer.pl line 18,
<PH> line 4.
Use of uninitialized value in hash element at customer.pl line 19,
<PH> line 4.
Use of uninitialized value in hash element at customer.pl line 17,
<PH> line 6.
Use of uninitialized value in hash element at customer.pl line 18,
<PH> line 6.
Use of uninitialized value in hash element at customer.pl line 19,
<PH> line 6.
Use of uninitialized value in hash element at customer.pl line 17,
<PH> line 8.
Use of uninitialized value in hash element at customer.pl line 18,
<PH> line 8.
Use of uninitialized value in hash element at customer.pl line 19,
<PH> line 8.
Use of uninitialized value in hash element at customer.pl line 17,
<PH> line 10.

Use of uninitialized value in hash element at customer.pl line 18,
<PH> line 10.

Use of uninitialized value in hash element at customer.pl line 19,
<PH> line 10.

Use of uninitialized value in hash element at customer.pl line 17,
<PH> line 12.

Use of uninitialized value in hash element at customer.pl line 18,
<PH> line 12.

Use of uninitialized value in hash element at customer.pl line 19,
<PH> line 12.

Use of uninitialized value in hash element at customer.pl line 17,
<PH> line 14.

Use of uninitialized value in hash element at customer.pl line 18,
<PH> line 14.

Use of uninitialized value in hash element at customer.pl line 19,
<PH> line 14.

Use of uninitialized value in hash element at customer.pl line 17,
<PH> line 16.

Use of uninitialized value in hash element at customer.pl line 18,
<PH> line 16.

Use of uninitialized value in hash element at customer.pl line 19,
<PH> line 16.

Use of uninitialized value in hash element at customer.pl line 17,
<PH> line 18.

Use of uninitialized value in hash element at customer.pl line 18,
<PH> line 18.

Use of uninitialized value in hash element at customer.pl line 19,
<PH> line 18.

Use of uninitialized value in hash element at customer.pl line 17,
<PH> line 20.

Use of uninitialized value in hash element at customer.pl line 18,
<PH> line 20.

Use of uninitialized value in hash element at customer.pl line 19,
<PH> line 20.

Use of uninitialized value in hash element at customer.pl line 17,
<PH> line 22.

Use of uninitialized value in hash element at customer.pl line 18,
<PH> line 22.

Use of uninitialized value in hash element at customer.pl line 19,
<PH> line 22.
************************************************** ****************************
17 $Name{$name} = $_;
18 $Phone{$number} = $_;
19 $Email{$email} = $_;
************************************************** **********************
so how can i initialize this value ---

************************************************** *************************
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      12-02-2004
(Mostafa) wrote in
news: om:

[ Snipped full-quote of previous message. Don't do that. Please read the
posting guidelines for comp.lang.perl.misc ]

> but i am getting some warnings , how can i get rid of this warnings .
> i also add following codes with my program ---
> use strict;
>
> my %Name;
> my %Phone;
> my %Email;
> my $name;
> my $number;
> my $email;
> my $address;
>
>
> i am giving u the list of warnings for the line 17,18,19. i add those
> lines right below the warnings.------
>
> C:\mp>perl customer.pl
> Use of uninitialized value in hash element at customer.pl line 17,
> <PH> line 2.


What is the point of posting a bazillion lines of essentially the same
warning without posting the actual runnable code that is producing it
along with a sample of the data?

Please read the posting guidelines for help on formulating your questions
so as to elicit useful answers.

--
A. Sinan Unur
d
(remove '.invalid' and reverse each component for email address)

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      12-02-2004
A. Sinan Unur <> wrote:
> (Mostafa) wrote in
> news: om:
>
> [ Snipped full-quote of previous message. Don't do that.



Because it will get you killfiled.

That's what I did when I saw it.


>> i am giving u the list of warnings

^
^

> Please read the posting guidelines



And use actual English.


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
a split is not a split Dumbell Computer Support 3 03-09-2009 10:45 PM
String#split(/\s+/) vs. String#split(/(\s+)/) Sam Kong Ruby 5 08-12-2006 07:59 PM
How can I split database results with ExecuteReader and Split? needin4mation@gmail.com ASP .Net 2 05-05-2006 10:36 PM
split on '' (and another for split -1) trans. (T. Onoma) Ruby 10 12-28-2004 06:36 AM
Small inconsistency between string.split and "".split Carlos Ribeiro Python 11 09-17-2004 05:57 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57