READFILE sorting coding problem

Discussion in 'Perl Misc' started by walfish@charter.net, Oct 25, 2013.

  1. Guest

    I am new to Perl and trying to read a file (employee.txt) that has four columns of data. I want to read from the file, print the first and last column, sorted by the first column. The code I have so far is below but when I run it, I only get a "1 file found" message, no data prints. Your help is greatly appreciated.

    #!/usr/bin/perl
    $i = 0;
    @names = open(READFILE, "employee.txt") || die "Couldn't open file: $!";

    foreach(sort sort_names (@names))
    {
    m/^\S*\s*(\S*)\s*\S*\s*\d{3}\.\d{3}\.\d{4}\s*(\S*)$/;
    print("$1\t$2\n");
    ++$i
    }
    print("$i records found\n");


    sub sort_names
    {
    $a =~ m/\S*\s*(\S*)/;
    $one = $1;
    $b =~ m/\S*\s*(\S*)/;
    $two = $1;

    $two cmp $one || $b <=> $a;
    }
    , Oct 25, 2013
    #1
    1. Advertising

  2. writes:
    > I am new to Perl and trying to read a file (employee.txt) that has four columns of data. I want to read from the file, print the first and last column, sorted by the first column. The code I have so far is below but when I run it, I only get a "1 file found" message, no data prints. Your help is greatly appreciated.
    >
    > #!/usr/bin/perl
    > $i = 0;
    > @names = open(READFILE, "employee.txt") || die "Couldn't open file: $!";


    This should be

    open(READFILE, "employee.txt") || die "Couldn't open file: $!";
    @names = <READFILE>;

    You want the contents of the file in @names, not the return value of
    open.
    Rainer Weikusat, Oct 25, 2013
    #2
    1. Advertising

  3. $Bill Guest

    On 10/25/2013 07:11, wrote:
    > I am new to Perl and trying to read a file (employee.txt) that has four columns of data. I want to read from the file, print the first and last column, sorted by the first column. The code I have so far is below but when I run it, I only get a "1 file found" message, no data prints. Your help is greatly appreciated.


    Something like this should work:

    use strict;
    use warnings;

    open READFILE, "employee.txt" or die "open employee.txt: $! ($^E)";
    my @names = <READFILE>;
    chomp @names;
    close READFILE;

    printf "records found = %s\n", scalar @names;

    foreach (sort { (split ' ', $a)[0] cmp (split ' ', $b)[0] } @names) {
    print "$i: $_\n" if $debug;
    printf "%s\t%s\n", (split ' ', $_)[0,3];
    }
    exit;

    __END__
    $Bill, Oct 25, 2013
    #3
  4. #!/usr/bin/perl
    use strict;
    use warnings;
    use feature 'say';
    #open DATA, '<:utf8', 'somefile.csv' or die "Could not read fiel because
    \"$^E\"\n";

    my $column_to_sort = 1;
    my @data;

    while (<DATA>) {
    /^\s*(\S*)[,\s]*(\S*?)[,\s]*(\d{3}\.\d{3}\.\d{4})[,\s]*(\S*?)\s*$/ or next;
    push @data, [$1,$2,$3,$4];
    }

    foreach (sort {$a->[0] cmp $b->[0] || $a->[3] cmp $b->[3]} @data) {
    say "@{$_}"
    }

    __DATA__
    name2 , colum1, 444.444.4444, info4
    name1 , colum3, 111.111.1111, info2
    name1 , colum2, 222.222.2222, info1
    name3 , colum4, 333.333.3333, info3
    George Mpouras, Oct 27, 2013
    #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. .Net Sports
    Replies:
    0
    Views:
    471
    .Net Sports
    Jan 11, 2006
  2. .Net Sports
    Replies:
    11
    Views:
    1,433
    .Net Sports
    Jan 17, 2006
  3. charly
    Replies:
    5
    Views:
    1,264
    charly
    Apr 23, 2004
  4. salsipius

    Help with ReadFile()

    salsipius, Oct 11, 2005, in forum: C++
    Replies:
    2
    Views:
    2,246
    John Harrison
    Oct 12, 2005
  5. Spacetech

    ReadFile Problem

    Spacetech, May 20, 2006, in forum: C++
    Replies:
    2
    Views:
    348
    Jonathan Mcdougall
    May 20, 2006
Loading...

Share This Page