Find the answer to your Linux question:
Results 1 to 3 of 3
Hello all I have a csv file filled with content like this 1111111|2222222|3333333 4444444|5555555|6666666 7777777|6666666|4444444 How can I read the content of the file and assign the first column value ...
  1. #1
    Just Joined!
    Join Date
    Aug 2009
    Location
    Toronto
    Posts
    31

    Perl help!

    Hello all

    I have a csv file filled with content like this

    1111111|2222222|3333333
    4444444|5555555|6666666
    7777777|6666666|4444444

    How can I read the content of the file and assign the first column value from each line as array element.

    To be something like

    @array = (1111111,4444444,777777)

    your help is highly appreciated

    Thank you

  2. #2
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    One method:
    Code:
    #!/usr/bin/env perl
    
    # @(#) p1	Demonstrate split.
    
    use strict;
    use warnings;
    
    my (@array) = ();
    
    while (<>) {
      push @array, ( split( /[|]/, $_, 2 ) )[0];
    }
    
    print " Content of array \"@array\"\n";
    
    exit(0);
    producing:
    Code:
    % ./p1 data1
     Content of array "1111111 4444444 7777777"
    See perldoc -f split for details.

    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  3. #3
    Just Joined!
    Join Date
    Aug 2009
    Location
    Toronto
    Posts
    31
    Quote Originally Posted by drl View Post
    Hi.

    One method:
    Code:
    #!/usr/bin/env perl
    
    # @(#) p1	Demonstrate split.
    
    use strict;
    use warnings;
    
    my (@array) = ();
    
    while (<>) {
      push @array, ( split( /[|]/, $_, 2 ) )[0];
    }
    
    print " Content of array \"@array\"\n";
    
    exit(0);
    producing:
    Code:
    % ./p1 data1
     Content of array "1111111 4444444 7777777"
    See perldoc -f split for details.

    Best wishes ... cheers, drl

    worked!

    Thank you so much! saved me lots of time!


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...