split commands oddity

Discussion in 'Perl' started by rxl124@hehe.com, Jan 28, 2004.

  1. Guest

    Hi, room
    Beginner of learning perl here!!

    I have question to all,

    I have below file name datebook.master which contains only 2 lines
    Mike wolf:12/3/44:144 park ave, paramus: 44000
    Sarah kim: 3/2/67:255 lomel ave, fort lee: 33000

    Now, I am testing out below scripts and working fine.
    -------------------- scripts 1------------------------------------------
    #!/usr/bin/perl -w
    open(FILE, "datebook.bak") || die "CAn't open $!\n";
    while(<FILE>) {
    @line=split(":");
    print "$line[0] $line[2]\n";
    }
    close(FILE)
    ------------------------------------------------------------------------

    And, I get desired results.
    However, if I do a script w/ below, it does not work.

    -------------------- scripts 2------------------------------------------
    #!/usr/bin/perl -w
    open(FILE, "datebook.bak") || die "CAn't open $!\n";
    @lines=(<FILE>);
    while(<@lines>) {
    @line=split(":");
    print "$line[0] $line[2]\n";
    }
    close(FILE)
    ------------------------------------------------------------------------

    Can someone tell me why this is happening?
    I want script 2 since I need to call this array again in the later parts of
    the scripts(in the longer version).

    Can someone kindly respond?

    Big thanks in advance.
    , Jan 28, 2004
    #1
    1. Advertising

  2. Gunnar Hjalmarsson, Jan 28, 2004
    #2
    1. Advertising

  3. wrote in message news:<>...
    > Hi, room
    > Beginner of learning perl here!!
    >
    > I have question to all,
    >
    > I have below file name datebook.master which contains only 2 lines
    > Mike wolf:12/3/44:144 park ave, paramus: 44000
    > Sarah kim: 3/2/67:255 lomel ave, fort lee: 33000
    >
    > Now, I am testing out below scripts and working fine.
    > -------------------- scripts 1------------------------------------------
    > #!/usr/bin/perl -w
    > open(FILE, "datebook.bak") || die "CAn't open $!\n";
    > while(<FILE>) {
    > @line=split(":");
    > print "$line[0] $line[2]\n";
    > }
    > close(FILE)
    > ------------------------------------------------------------------------
    >
    > And, I get desired results.
    > However, if I do a script w/ below, it does not work.
    >
    > -------------------- scripts 2------------------------------------------
    > #!/usr/bin/perl -w
    > open(FILE, "datebook.bak") || die "CAn't open $!\n";
    > @lines=(<FILE>);
    > while(<@lines>) {
    > @line=split(":");
    > print "$line[0] $line[2]\n";
    > }
    > close(FILE)
    > ------------------------------------------------------------------------
    >
    > Can someone tell me why this is happening?
    > I want script 2 since I need to call this array again in the later parts of
    > the scripts(in the longer version).
    >
    > Can someone kindly respond?
    >
    > Big thanks in advance.


    Change while(<@lines>) to foreach (@lines)

    ++imanshu.
    Himanshu Garg, Jan 28, 2004
    #3
  4. Guest

    (Himanshu Garg) wrote in message news:<>...
    > wrote in message news:<>...
    > > Hi, room
    > > Beginner of learning perl here!!
    > >
    > > I have question to all,
    > >
    > > I have below file name datebook.master which contains only 2 lines
    > > Mike wolf:12/3/44:144 park ave, paramus: 44000
    > > Sarah kim: 3/2/67:255 lomel ave, fort lee: 33000
    > >
    > > Now, I am testing out below scripts and working fine.
    > > -------------------- scripts 1------------------------------------------
    > > #!/usr/bin/perl -w
    > > open(FILE, "datebook.bak") || die "CAn't open $!\n";
    > > while(<FILE>) {
    > > @line=split(":");
    > > print "$line[0] $line[2]\n";
    > > }
    > > close(FILE)
    > > ------------------------------------------------------------------------
    > >
    > > And, I get desired results.
    > > However, if I do a script w/ below, it does not work.
    > >
    > > -------------------- scripts 2------------------------------------------
    > > #!/usr/bin/perl -w
    > > open(FILE, "datebook.bak") || die "CAn't open $!\n";
    > > @lines=(<FILE>);
    > > while(<@lines>) {
    > > @line=split(":");
    > > print "$line[0] $line[2]\n";
    > > }
    > > close(FILE)
    > > ------------------------------------------------------------------------
    > >
    > > Can someone tell me why this is happening?
    > > I want script 2 since I need to call this array again in the later parts of
    > > the scripts(in the longer version).
    > >
    > > Can someone kindly respond?
    > >
    > > Big thanks in advance.

    >
    > Change while(<@lines>) to foreach (@lines)
    >
    > ++imanshu.



    thank you all, I have used for and it's working like charms..
    I was doing excersise on the book that I recently purchased so that I
    can practice on perl by myself and it was getting tough to do this
    particular one cause I couldn't understand what was going on. Below is
    entire script(please do advise as I am real beginner and if I made
    really short script long one.)

    also, this --> @all_lines = map { split(":") } @lines; , not
    understanding
    why split command would not work w/out map.......

    At all rate, thank you very much.


    ----------------------------------------------------------------
    #!/usr/bin/perl

    open (FILEINFO, "file.txt") || die "Can't open file $!\n";
    @lines=<FILEINFO>;
    close FILEINFO;
    @all_lines = map { split(":") } @lines;

    print "Who would you like to find? \n";
    chomp($looking_for=<STDIN>) ;
    print "You are looking for $looking_for \n";
    $count_look = grep(/$looking_for/i, @all_lines);
    @count_look = grep(/$looking_for/i, @all_lines);

    print " Number of person that matched was $count_look \n";
    print " They are @count_look \n";


    for (@lines) {
    ( $name, $phone, $address, $bd, $sal )=split(":");
    print "$name\t $phone\n";
    }

    sleep(1);
    print "Who are you searching for ?";
    chomp($looking_for1=<STDIN>) ;
    @looking_for2=grep(/$looking_for1/i, @lines);
    $looking_for2=grep(/$looking_for1/i, @lines);
    ( $name_l, $phone_l, $address_l, $bd_l, $sal_l )=map {split(":") }
    @looking_for2;
    print "@looking_for2\n";
    sleep(1);
    print "What is the new phone number for $looking_for1 ?";
    chomp($new_numb=<STDIN>);
    print "$looking_for1\'s phone number is currently $phone_l\n";
    @newinfo = split(":", @looking_for2);
    @newinfo1 = splice(@newinfo, 1,1, "$new_numb");

    print "Here is the line showing the new phone number:\n";
    print join(":", $name_l, $new_numb, $address_l, $bd_l, $sal_l), "\n";
    print "$looking_for1 was found in the array $looking_for2 times. \n";
    , Jan 29, 2004
    #4
    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:
    445
  2. Ben Pfaff

    Re: man pages for C commands (GCC commands)

    Ben Pfaff, Jun 24, 2003, in forum: C Programming
    Replies:
    4
    Views:
    3,942
    Thomas Stegen
    Jun 28, 2003
  3. Tim Stanka
    Replies:
    1
    Views:
    781
    Jeff Epler
    Aug 2, 2004
  4. Carlos Ribeiro
    Replies:
    11
    Views:
    677
    Alex Martelli
    Sep 17, 2004
  5. trans.  (T. Onoma)

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

    trans. (T. Onoma), Dec 27, 2004, in forum: Ruby
    Replies:
    10
    Views:
    194
    Florian Gross
    Dec 28, 2004
Loading...

Share This Page