On Tue Mar 10 2009 @  4:13, Meghanand Acharekar wrote:
> Hi,
> 
> Need some help
> How can I parse a file line by line using perl.
> 
> I want to parse a test file having following data format
> 
> *File : user_stats.txt*
> 20GB      Larry
> 14.5MB   Bob
> 3MB        John
> 
> so that I can send this data to a MySQL database table.
> Can I use while loop (any other loop control) for this ?
> 

Yes, you can use a while loop to work on a file line by line

    #!/usr/bin/env perl
    use strict;
    use warnings;

    open my $data_file, '<', 'user_stats.txt'
      or die "Can't open 'user_stats.txt' for reading: $!";

      while (my $line = <$data_file>) {
        next if $line =~ m/^\*File/;
        my @fields = split /\s+/, $line;
        print "@fields\n";
      }

You would need to adjust for the proper path to user_stats.txt, and the
code inside the while loop is just for show. 

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to