Thread: Help Needed!

Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2014
    Posts
    7
    Rep Power
    0

    Help Needed!


    Hello,

    I just started learning perl this week. Nothing seems to be working for me when I try to follow steps from any online tutorials.

    I am in windows7 using Strawberry to try to manipulate data.txt files. My script does not do anything when I try to run it in Strawberry. I have the script and the data.txt file I want to manipulate in the same folder on the hard drive. Strawberry program itself was installed somewhere else.

    Why when I run this script below, nothing happens at all, all the examples online look like this open command:
    #!/usr/bin/perl
    use warnings;
    use diagnostics;

    open(FILE1, "data.txt") or die "Couldn't open file data.txt, $!";

    while(<FILE1>){
    print $_;
    }

    When when I run the script like below, it works???
    #!/usr/bin/perl
    use warnings;
    use diagnostics;

    open(FILE1, 'C:\perl_tests\data.txt') or die "Couldn't open file data, $!";

    while(<FILE1>){
    print "$_";
    }

    This script works but I can't do anything the data.txt file except to open and print. I need to copy all the lines after the 2nd line of this data.txt file to a new data.txt file and then sort the new data.txt file by the first column. First column of new data file will have all numbers and need to be sorted in ascending order.

    I can't use modules either.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    776
    Rep Power
    495
    The first version of your program should work if you launch it from the directory where the data.txt file is located and should fail if it is not there. If it does not fail but does not do anything, then it would appear that you have an empty data.txt file in your current directory.

    Having said that, it appears that the tutorials you are using are either not so good or quite outdated. For example, the modern way to open a file for reading it would be something like this:

    perl Code:
    my $data_file = "data.txt";
    open my $FILE1, "<", $data_file or die "Couldn't open file $data_file, $!";
    while (<$FILE1>) {
         # ...

    If you want to copy your data to another file, then you need to open a second file and print to the file handler of this second file:

    perl Code:
    my $data_file = "data.txt";
    open my $FILE1, "<", $data_file or die "Couldn't open file $data_file, $!";
    my $out_file = "out_data.txt";
    open my $FILE_OUT, ">", $out_file or die "Couldn't open file $out_file, $!";
    while (<$FILE1>) {
         print $FILE_OUT "$_";
    }


    As for the sorting, please provide a sample of your data file, it is not possible to say how to do it without having seen the data format.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2014
    Posts
    7
    Rep Power
    0

    Sample data.txt


    Thanks! Your suggestions worked!

    ------------- data.txt ------------------------------------
    $ NUMBR/PKGS/SERIAL/N_AUTN/ABc/POV

    80117900000000000001 321421641164301 1111122222 00112233445566778899AABBCCDDEEFF 00112233445566778899AABBCCDDEEFF 00112233445566778899AABBCCDDEEFF
    80117900000000000011 321421641164311 3333344444 00112233445566778899AABBCCDDEEFF 00112233445566778899AABBCCDDEEFF 00112233445566778899AABBCCDDEEFF
    80117900000000000002 321421641164302 5555566666 00112233445566778899AABBCCDDEEFF 00112233445566778899AABBCCDDEEFF 00112233445566778899AABBCCDDEEFF
    80117900000000000031 321421641164331 7777788888 00112233445566778899AABBCCDDEEFF 00112233445566778899AABBCCDDEEFF 00112233445566778899AABBCCDDEEFF
    80117900000000000020 321421641164320 9999900000 00112233445566778899AABBCCDDEEFF 00112233445566778899AABBCCDDEEFF 00112233445566778899AABBCCDDEEFF
    80117900000000000100 321421641164100 1111133333 00112233445566778899AABBCCDDEEFF 00112233445566778899AABBCCDDEEFF 00112233445566778899AABBCCDDEEFF
    80117900000000000055 321421641164355 4444466666 00112233445566778899AABBCCDDEEFF 00112233445566778899AABBCCDDEEFF 00112233445566778899AABBCCDDEEFF
    80117900000000000003 321421641164303 7777799999 00112233445566778899AABBCCDDEEFF 00112233445566778899AABBCCDDEEFF 00112233445566778899AABBCCDDEEFF


    For better viewing, you may want to copy and paste into Notepad++. The first two lines ($ and blank line) need to be removed before sorting by the the first column (the data in each line following the number in the first column must go where it goes also).
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,875
    Rep Power
    1225
    If the data file fits in memory, I'd load it into an AoA or a hash (if there aren't any dups in sorting column) and use perl's sort function do the sorting prior to outputting the data to the new file.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    9
    Rep Power
    0
    If the column to be sorted is the same length as in your sample data, then the following would accomplish your task.
    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    
    my $read = "data.txt";
    open my $FILE1, $read or die "Couldn't open file $read, $!";
    
    my $new = 'newfile.txt';
    open my $out, '>', $new or die "Unable to write $new because $!";
    
    <$FILE1> for 1 .. 2; # throw away first 2 lines
    print $out sort <$FILE1>; # print sorted to new file
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    776
    Rep Power
    495
    Yes, if the first column is always the same length and has always the indicated format, then it can be as simple as the solution described by Chris (and this is exactly the reason why I asked for a data format).

    If the length of the first column can vary from one record to another, then it is very slightly more complicated, and I would probably go for a solution consisting in loading the file into a simple hash and sorting its keys before printing the content out (provided that some conditions are satisfied, namely that the first field is guaranteed to be unique and that the file is not too large to fit into memory).
    Last edited by Laurent_R; February 6th, 2014 at 05:01 PM.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2014
    Posts
    7
    Rep Power
    0

    Thank you!


    Probably not the most efficient, but I got it working with the rest of the script.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    776
    Rep Power
    495
    Originally Posted by foonghuang
    Probably not the most efficient
    Why do you think so? I think it would be quite difficult to make something more efficient.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2014
    Posts
    7
    Rep Power
    0

    It's fine for me, but this is why I wonder about efficiency


    I had to create another file in the middle of the script to make it work, so I end up with 2 output files and only the last one is the one I want.

    I don't understand the logistics of why after I opened the 2nd file to copy to that I cannot manipulate it since I am thinking that technically, it is already open?

    Here is the final script I have.

    #!/usr/bin/perl
    # SortNewFile.pl
    use warnings;
    use diagnostics;

    open(FILE1, "<Output.txt") or die "Couldn't open file Output.txt, $!";

    # Open new file to write
    open(FILE2, ">copytext.txt");

    while(<FILE1>){
    print FILE2 $_;
    }

    print "hello\n";

    close( FILE1 );
    close( FILE2 );

    my $read = "copytext.txt";
    open my $FILE1, $read or die "Couldn't open file $read, $!";

    my $new = 'Original.txt';
    open my $out, '>', $new or die "Unable to write $new because $!";

    <$FILE1> for 1 .. 2; # throw away first 2 lines
    print $out sort <$FILE1>; # print sorted to new file

    close( FILE1 );
    close( FILE2 );
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,875
    Rep Power
    1225
    Most of what you're doing in that script is being done oddly and very inefficient and that is very understandable for being a beginner.

    I can't use modules either.
    Why not? Is it because you're taking a class and that is one of the restrictions imposed by the instructor? If that's not the case, then there is no logical reason why you can't use a module.

    Here's one method to efficiently edit the file.
    Code:
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    use diagnostics;
    
    my $file = 'data.txt';
    open my $fh, '+<', $file or die "Failed to open '$file' $!";
    
    my @file_content = <$fh>;
    splice @file_content, 0, 2;
    
    seek $fh, 0,0;
    truncate $fh, 0;
    
    print $fh sort @file_content;
    close $fh;
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2014
    Posts
    7
    Rep Power
    0

    Can't use modules..


    I was told that I can't use modules because the perl script will be run on a UNIX-based server and we cannot load modules into it.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,875
    Rep Power
    1225
    Not being able to install modules would be due to stupid company policies, not technical reasons.

    Perl comes with many core modules that can be used to aide in this and many other tasks. Tie::File is one such module.

    That being said, modules are not really needed in this case as shown by the code I posted. There are several other optional approaches that don't require the use of a module.

    Here's a slightly modified version of the script I posted.
    Code:
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    use diagnostics;
    
    my $file = 'data.txt';
    open my $fh, '<', $file or die "Failed to open '$file' $!";
    
    my @file_content = <$fh>;
    close $fh;
    
    splice @file_content, 0, 2;
    
    open my $new_fh, '>', $file or die "Failed to open '$file' $!";
    print $new_fh sort @file_content;
    close $new_fh;
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,875
    Rep Power
    1225
    Code:
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    use diagnostics;
    use Tie::File;
    
    my $file = 'data.txt';
    tie my @file_content, 'Tie::File' or die "camn't tie $file $!";
    
    splice @file_content, 0, 2;
    untie @file_content;
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2011
    Posts
    53
    Rep Power
    3

    Regex Help


    I'd like to search for a word up to a certain point within a sentence and exit on that word. For instance:

    here is a sentence that I will use a example

    I want to search for all 'a' before sentence and exit the script if an a is found. Is it as simple as:

    Code:
    while ($word =~ /a.+sentence/mxsi){
    exit;
    }
    else{
    return;
    }
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,875
    Rep Power
    1225
    dolfantb, please do not hijack threads. You need to start a new thread for your question.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo