split function syntax

M

Mostafa

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 (248)-555-9430 (e-mail address removed)

Hunter,Apryl (810)-555-3029 (e-mail address removed)

Stewart,Pat (405)-555-8710 (e-mail address removed)

Ching,Iris (305)-555-0919 (e-mail address removed)

Doe,John (212)-555-0912 (e-mail address removed)

Jones,Tom (312)-555-3321 (e-mail address removed)

Smith,John (607)-555-0023 (e-mail address removed)

Crosby,Dave (405)-555-1516 (e-mail address removed)

Johns,Pam (313)-555-6790 (e-mail address removed)

Jeter,Linda (810)-555-8761 (e-mail address removed)

Garland,Judy (305)-555-1231 (e-mail address removed)
***********************************************************************
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?????????
 
S

Sherm Pendley

Mostafa said:
($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--
 
A

A. Sinan Unur

(e-mail address removed) (Mostafa) wrote in

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::Dumper 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::Dumper;
print Dumper \%Phone, \%Email;

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

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

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

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::Dumper;
print Dumper \%Phone, \%Email, \%Name;

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


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

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

Tad McClellan

Mostafa said:
($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.
 
M

Michele Dondi

^^^^
^^^^

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
 
M

Mostafa

A. Sinan Unur said:
(e-mail address removed) (Mostafa) wrote in

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::Dumper 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::Dumper;
print Dumper \%Phone, \%Email;

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

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

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

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::Dumper;
print Dumper \%Phone, \%Email, \%Name;

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


C:\Home\asu1> ph
$VAR1 = {
'(405)-555-8710' => 'Stewart,Pat (405)-555-8710 (e-mail address removed)',
'(305)-555-0919' => 'Ching,Iris (305)-555-0919 (e-mail address removed)',
'(810)-555-3029' => 'Hunter,Apryl (810)-555-3029 (e-mail address removed)'
};
$VAR2 = {
'(e-mail address removed)' => 'Ching,Iris (305)-555-0919 (e-mail address removed)',
'(e-mail address removed)' => 'Hunter,Apryl (810)-555-3029 (e-mail address removed)',
'(e-mail address removed)' => 'Stewart,Pat (405)-555-8710
(e-mail address removed)'
};
$VAR3 = {
'Hunter,Apryl' => 'Hunter,Apryl (810)-555-3029 (e-mail address removed)',
'Ching,Iris' => 'Ching,Iris (305)-555-0919 (e-mail address removed)',
'Stewart,Pat' => 'Stewart,Pat (405)-555-8710 (e-mail address removed)'
};
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 ---

***************************************************************************
 
A

A. Sinan Unur

(e-mail address removed) (Mostafa) wrote in

[ 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.
 
T

Tad McClellan

A. Sinan Unur said:
(e-mail address removed) (Mostafa) wrote in

[ 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.

^
^

Please read the posting guidelines


And use actual English.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,269
Messages
2,571,099
Members
48,773
Latest member
Kaybee

Latest Threads

Top