split function syntax

Discussion in 'Perl Misc' started by Mostafa, Dec 1, 2004.

  1. Mostafa

    Mostafa Guest

    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

    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?????????
     
    Mostafa, Dec 1, 2004
    #1
    1. Advertising

  2. 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
     
    Sherm Pendley, Dec 1, 2004
    #2
    1. Advertising

  3. (Mostafa) wrote in
    news::

    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
    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::Dumper;
    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)
     
    A. Sinan Unur, Dec 1, 2004
    #3
  4. 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
     
    Tad McClellan, Dec 1, 2004
    #4
  5. 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,
     
    Michele Dondi, Dec 1, 2004
    #5
  6. Mostafa

    Mostafa Guest

    "A. Sinan Unur" <> wrote in message news:<Xns95B1E9DD6A3D7asu1cornelledu@132.236.56.8>...
    > (Mostafa) wrote in
    > news::
    >
    > 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
    > 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::Dumper;
    > 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 ---

    ***************************************************************************
     
    Mostafa, Dec 1, 2004
    #6
  7. (Mostafa) wrote in
    news::

    [ 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)
     
    A. Sinan Unur, Dec 2, 2004
    #7
  8. A. Sinan Unur <> wrote:
    > (Mostafa) wrote in
    > news::
    >
    > [ 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
     
    Tad McClellan, Dec 2, 2004
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    492
  2. Carlos Ribeiro
    Replies:
    11
    Views:
    742
    Alex Martelli
    Sep 17, 2004
  3. trans.  (T. Onoma)

    split on '' (and another for split -1)

    trans. (T. Onoma), Dec 27, 2004, in forum: Ruby
    Replies:
    10
    Views:
    241
    Florian Gross
    Dec 28, 2004
  4. Sam Kong
    Replies:
    5
    Views:
    279
    Rick DeNatale
    Aug 12, 2006
  5. Stanley Xu
    Replies:
    2
    Views:
    713
    Stanley Xu
    Mar 23, 2011
Loading...

Share This Page