bh_perl has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

Could somebody guide me, how could i open the input files and split it by line ?

As example, This is the input file (sample.txt):-

#---------------------------------------- # Note: # Input format: <Name>:<value> #---------------------------------------- name:00 00 0D mobile:123455 name:00 00 30 mobile:6575758

Then, how could i open the files and read based on the new line separator (/\n/) ?

Edit: g0n - removed pre tags. Formatting.

Replies are listed 'Best First'.
Re: how to split based on new line or others ?
by ikegami (Patriarch) on Nov 08, 2006 at 06:54 UTC
    Are you asking how to read the file a line at a time?
    my $file_name = ...; open(my $fh, '<', $file_name) or die("Unable to open file \"$file_name\": $!\n"); while (<$fh>) { # $_ contains a line, including the "\n". # Use "chomp;" to remove the trailing "\n". ... }

    By the way, please use <c>...</c> instead of <pre>...</pre> on PerlMonks.

Re: how to split based on new line or others ?
by bobf (Monsignor) on Nov 08, 2006 at 07:11 UTC
Re: how to split based on new line or others ?
by prasadbabu (Prior) on Nov 08, 2006 at 06:54 UTC

    bh_perl,

    To accomplish your work, you have to use split and open. After writing the code, if you find any problem, post your code here and all will help you definitely. Or else you can read line by line using while statement.

    Prasad

      Ok... This is my code:-
      #!/usr/bin/perl use Getopt::Std; use vars qw /%opt/; my %fldinfo; my $options = 'h:i:o:'; getopts("$options", \%opt) or usage(); sub usage { print "Usage: $0 -[h] -i <input_file>\n"; if ($opt{h}) { print " -h : help\n"; print " -i : input value to be created\n"; print " -o : output-file\n"; } } sub chk_info { while (my ($fldname, $fldvalues) = each (%fldinfo)) { print "<$fldname>$fldvalues</$fldname>\n"; } } main { if ($opt{h}) { usage(); } open(DATA, "$opt{i}"); while (my $data = <DATA>) { next if $data =~ /^#/; foreach (split(/\n/,$data)) { my ($fldname, $fldvalue) = (split(/:/,$_))[0,1 +]; $fldinfo{$fldname} = $fldvalue; } } close(DATA); chk_info(); exit(); }
      I have tried to open and split the input data based on the new line. But the program did not captured or print out all the input data of th +e input file. It only print out the last record of the files. As example:- name:00 00 30 mobile:6575758

        bh_perl

        The problem is not in splitting but in storing the keys in hash. Hash keys are always unique, but in your case (keys: name and mobile) you are using duplicate keys. So the final key and values are getting printed. If you tell the exact requirement, we can correct you.

        Check with the print statements:

        foreach (split(/\n/,$data)) { my ($fldname, $fldvalue) = (split(/\:/,$_))[0,1]; print "**$fldname**\t$fldvalue\n"; $fldinfo{$fldname} = $fldvalue; }

        Prasad

        You're asking to split input data on newlines, but you are reading the data a line at a time. So, your split (at

        foreach (split(/\n/,$data)) {)

        is "splitting" the $data into two pieces: everything before the newline and everything after. Since the newline is at the end of the line (by definition....), there's not much in the second chunk.

        Now, when splitting the chunks of $data, it will split them into a chunk ($fldname, with everything before the first colon(":"), and $fldvalue will contain everything after the first colon.

        So, the split on newline is superfluous, unless you reset $/ (see perlvar).

        emc

        At that time [1909] the chief engineer was almost always the chief test pilot as well. That had the fortunate result of eliminating poor engineering early in aviation.

        —Igor Sikorsky, reported in AOPA Pilot magazine February 2003.
Re: how to split based on new line or others ?
by smokemachine (Hermit) on Nov 08, 2006 at 11:37 UTC
    Could be this?!
    perl -F: -nae 'next if /^#|^$/;chomp$F[1];print "<$F[0]>$F[1]</$F[0]>\ +n"' file