in reply to how to store text file information into my sql database

If you have fixed width records then use unpack

#!perl use strict; while (<DATA>){ chomp; my @vals = unpack("A10A10A5A24A10",$_); print join "|", @vals,"\n"; #$sth->execute(@vals); } __DATA__ StudentId Name Dept address city 1 Chellappa CSE 22 xx-colony 2nd street coimbatore 2 Vijay IT 23 yy colony coimbatore
poj

Replies are listed 'Best First'.
Re^2: how to store text file information into my sql database
by Anonymous Monk on Dec 26, 2015 at 19:11 UTC

    You can also generate the unpack string from the header itself if you know that the position of the keys in the header defines the columns in the rest of the file:

    #!/usr/bin/perl use strict; use warnings; my $header = <DATA>; my @p; push @p, length $1 while $header =~ /(\S+\s*)/g; my $unpack_pattern = join "", map { 'A'.$_ } @p[0..$#p-1]; $unpack_pattern .= "A*"; while (<DATA>){ chomp; my @vals = unpack($unpack_pattern,$_); print join "|", @vals,"\n"; #$sth->execute(@vals); } __DATA__ StudentId Name Dept address city 1 Chellappa CSE 22 xx-colony 2nd street coimbatore 2 Vijay IT 23 yy colony coimbatore

      There is a CPAN module DataExtract::FixedWidth for handling the fixed unpacking details. I tried it on the test sample and it seems to work fine.

      use strict; use warnings; use DataExtract::FixedWidth; use Data::Dumper; my $de = DataExtract::FixedWidth->new({ header_row => scalar(<DATA>) }); while (my $l = <DATA>) { my $fields = $de->parse($l); print Dumper($fields); } __DATA__ StudentId Name Dept address city 1 Chellappa CSE 22 xx-colony 2nd street coimbatore 2 Vijay IT 23 yy colony coimbatore
      Ron
Re^2: how to store text file information into my sql database
by chella2104@gmail.com (Sexton) on Dec 28, 2015 at 05:00 UTC

    StudentId | Name | Dept | address | city |

    +-----------+--------+--------+-----------+------------------------+

    | 1 | a CSE | 22 x | x-colony | 2nd street coimbatore |

    | 2 | T 23 | yy col | ony coim | batore

    when i am trying this code only above values to store database for example CSE is not stored in dept column