Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

How to store multiples lines/records in a array

by Perlseeker_1 (Acolyte)
on Jun 28, 2013 at 17:05 UTC ( #1041289=perlquestion: print w/ replies, xml ) Need Help??
Perlseeker_1 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I am reading every record from csv file and stroing them in array and then printing each record in table

My Input is:

ABC_TEK 12 DFE_YJK 24 JKL_GHY 26 LKJ_UYH 29 GHK_YTH 23 GHS_JSF 34 FHK_THE 25 SHJ_TRE 26 HSJ_TEH 39 AHJ_EGH 56 SGH_HEJ 42 HGE_THE 46 HEJ_EJJ 67 JLE_EJK 89 EHJ_EJK 78 EHJ_IKL 68 EHJ_EJK 57 EJH_EKL 69

I am tired with code shown below:

{ my @values; my $table; open (F, "Filename") || die ("Could not open $Filename!"); while (my $line = <F>) {chomp($line); push (@values,split(',', $line));} close (F); $table .= "<table border=1>"; for (my $i=1;$i<4;$i++) { $table .= "<tr>"; for (my $j=1;$j<7;$j++) { $table .= "<td>$values[$j]</td>";} $table .= "</tr>";} $table .= "</table>";}

gives me this output message / faulty output:

ABC_TEK 12 DFE_YJK 24 JKL_GHY 26 LKJ_UYH 29 GHK_YTH 23 GHS_JSF 34 FHK_ +THE 25 SHJ_TRE 26 HSJ_TEH 39 AHJ_EGH 56 SGH_HEJ 42 HGE_THE 46 HEJ_EJJ + 67 JLE_EJK 89 EHJ_EJK 78 EHJ_IKL 68 EHJ_EJK 57 EJH_EKL 69

I'm trying to print records one after the other in table format

Regard,

Comment on How to store multiples lines/records in a array
Select or Download Code
Re: How to store multiples lines/records in a array
by toolic (Chancellor) on Jun 28, 2013 at 17:13 UTC
      Hi,

      Please check the input below:

      ABC_TEK,12,DFE_YJK,24,JKL_GHY,26,LKJ_UYH,29,GHK_YTH,23,GHS_JSF,34, FHK_THE,25,SHJ_TRE,26,HSJ_TEH,39,AHJ_EGH,56,SGH_HEJ,42,HGE_THE,46, HEJ_EJJ,67,JLE_EJK,89,EHJ_EJK,78,EHJ_IKL,68,EHJ_EJK,57,EJH_EKL,69,

      I am tired with code shown below (subroutine-data):

      data{ my @values; my $table; open (F, "Filename") || die ("Could not open $Filename!"); while (my $line = <F>) { chomp($line); push (@values,split(',', $line));} close (F); $table .= "<table border=1>"; for (my $i=1;$i<4;$i++) { $table .= "<tr>"; for (my $j=1;$j<7;$j++) { $table .= "<td>$values[$j]</td>";} $table .= "</tr>";} $table .= "</table>";} }

      I am calling the subroutine data as mentioned below:

      my $table = Data(); Email( $table); <> <p>gives me this output message / faulty output: in table </p> <code> ABC_TEK 12 DFE_YJK 24 JKL_GHY 26 LKJ_UYH 29 GHK_YTH 23 GHS_JSF 34 FHK_ +THE 25 SHJ_TRE 26 HSJ_TEH 39 AHJ_EGH 56 SGH_HEJ 42 HGE_THE 46 HEJ_EJJ + 67 JLE_EJK 89 EHJ_EJK 78 EHJ_IKL 68 EHJ_EJK 57 EJH_EKL 69

      I am putting all the read from the array in table and calling subroutine email to print the data in table format in the body

      Please check why i was not able to print all records

Re: How to store multiples lines/records in a array
by Laurent_R (Parson) on Jun 28, 2013 at 17:32 UTC

    I answered essentially the same thing as toolic on your cross post on the Perl Gurus: you are splitting on commas and your data elements are separated by spaces.

Re: How to store multiples lines/records in a array
by marto (Chancellor) on Jun 28, 2013 at 17:32 UTC

    In addition to what toolic said, you seem not to be learning from your past experience. In one of your previous threads I gave you links to help learn the basics. Here you seem to be trying things at random and reporting that they don't do what you want. This is a poor strategy, and while it may seem to take less effort than actually learning how to use the tools you've chosen to work with, trust me, it doesn't.

    Here you can't have posted the code which produces this output, you split on commas when your input doesn't have any, you don't ask open to tell you why it may fail (e.g. or die "cannot open < $Filename: $!";), and crucially there is no print or say!

    In the interests of actually getting better at this I suggest you read and more importantly understand the links provied in your previous thread. When posting it's important to show your code, the actual code in you're running, the actual input data and any output or errors generated. This is all covered in How do I post a question effectively?.

Re: How to store multiples lines/records in a array
by ww (Bishop) on Jun 28, 2013 at 17:38 UTC
    • Your data fields appear to be tab or space separated which makes splitting on commas somewhat less than optimal.
    • Ln 4 offers us a Filename and a var, $Filename. That's a few short of a dozen, too. In general, such goofs may be the result of re-typing your code in your post. Suggest you use cut'n'paste, instead.
    • $Filename and $line are undeclared.
    • The indentation on your table-building stanza makes it hard to read; not a big deal here, but in a more complex or longer code segment it would be a problem. Similarly, ending each table, tr, & td tag with a newline would make the resultant html more readable.

    If you didn't program your executable by toggling in binary, it wasn't really programming!

      Hi Monks,

      I am reading records from the file and assigning the each record to array using the below code and trying to print each field in table

      data{ my @values; my $table; my $Filename; open (F, "$Filename") || die ("Could not open $Filename!"); while (my $line = <F>) { chomp($line); push (@values,split(',', $line));} close (F); $table .= "<table border=1>"; for (my $i=1;$i<4;$i++) { $table .= "<tr>"; for (my $j=1;$j<7;$j++) { $table .= "<td>$values[$j]</td>";} $table .= "</tr>";} $table .= "</table>";} } my $table = Data(); Email( $table); sub Email { my ( $table) = @_; my $lEmailAddress = 'hem_k24@gmail.com'; my $env = "- $ENV_NAME"; my $msg = MIME::Lite->new( From => $lEmailAddress, To => $lEmailAddress, Cc => $lEmailAddress, Subject => "Test", Type => 'multipart/mixed', ); $msg->attach( Type => 'TEXT/HTML', Data => $table, ); $msg->send; } <p>This is my input</p> <code> ABC_TEK,12,DFE_YJK,24,JKL_GHY,26,LKJ_UYH,29,GHK_YTH,23,GHS_JSF,34, FHK_THE,25,SHJ_TRE,26,HSJ_TEH,39,AHJ_EGH,56,SGH_HEJ,42,HGE_THE,46, HEJ_EJJ,67,JLE_EJK,89,EHJ_EJK,78,EHJ_IKL,68,EHJ_EJK,57,EJH_EKL,69,

      gives me this output message / faulty output: in table

      ABC_TEK 12 DFE_YJK 24 JKL_GHY 26 LKJ_UYH 29 GHK_YTH 23 GHS_JSF 34 FHK_ +THE 25 SHJ_TRE 26 HSJ_TEH 39 AHJ_EGH 56 SGH_HEJ 42 HGE_THE 46 HEJ_EJJ + 67 JLE_EJK 89 EHJ_EJK 78 EHJ_IKL 68 EHJ_EJK 57 EJH_EKL 69

      I'm trying to print the fields in table

        So, so far you appear to have dealt with only half the suggestions I offered/problems I mentioned.

        And, you've changed the specification for the data. I suppose that counts too ...so call it three out of four, although that doesn't entirely account for your handling of the observations/recommendations from other Monasterians, above. Their time and effort on your behalf seems to have been been given short shrift, too.

        But -- at least as far as my notes go -- that's OK. I really wrote the rest solely to occupy myself during a slow period between Lion-baitings and bug hunts.

        : - (

        If you didn't program your executable by toggling in binary, it wasn't really programming!

Re: How to store multiples lines/records in a array
by NetWallah (Abbot) on Jun 28, 2013 at 18:00 UTC
    In addition to what others have said, did you really intend to index the arrays starting at 1 ? Perl arrays start at 0. You seem to want to print 6 itemes per row, and starting at a "1" index will leave unprinted items.

    Also, perl loops are usually written in a way that avoids indexing. Your loops could be written (assuming you want a 0-based index, and want to print the complete input data, and you correct the input 'split' delimiter):

    # HTML ignores the "\n", but it looks nicer in 'print'; $table="<table>\n"; for my $i(0..5) { $table .= "<tr>"; for (@values[$i*6 .. ($i*6+5)]) { # 'Array Slice' $table .= "<td>$_</td>"; } $table .= "</tr>\n"; } $table .= "</table>\n"
    And there is still room for improvement once you learn about "map" and "join" functions, as well as the Text::CSV and CGI modules.

                 "The trouble with the Internet is that it's replacing masturbation as a leisure activity."
            -- Patrick Murray

      Hi Monks,

      Thanks for the replies, its working when the length of record is 5

      code suggested by experts

      # HTML ignores the "\n", but it looks nicer in 'print'; $table="<table>\n"; for my $i(0..5) { $table .= "<tr>"; for (@values[$i*6 .. ($i*6+5)]) { # 'Array Slice' $table .= "<td>$_</td>"; } $table .= "</tr>\n"; } $table .= "</table>\n"

      output displayed in table

      ABC_TEK 12 DFE_YJK 24 JKL_GHY 26 HJK_YHJ 46 LKJ_UYH 29 GHK_YTH 23 GHS_JSF 34 GHJ_YJK 56 FHK_THE 25 SHJ_TRE 26 HSJ_TEH 39 SGH_YHK 66 AHJ_EGH 56 SGH_HEJ 42 HGE_THE 46 YHJ_JKL 44 HEJ_EJJ 67 JLE_EJK 89 EHJ_EJK 78 GSH_THK 43 EHJ_IKL 68 EHJ_EJK 57 EJH_EKL 69 HGJ_HJK 44

      but if the size of the record increased from 5 to 6, i hope i need to change the logic from $i(0..6) and for (@values$i*7 .. ($i*7+6)), is thier any way where we can fetch each line based on line size

      <code> for my $i(0..$#line) <code>

      please give suggestion on this

        is thier any way where we can fetch each line based on line size

        Sure, a)determine line size (some kind of number) b) store size in a variable (some kind of number) c) use variable instead of hard coded number ( 5 or 6)

        Learn how the code you were given works. Past answers to you explain debugging and learning programming.
        scalar (@values) will give you the number of elements in @values.

        Since you are printing 6 per line, the number of lines will be:

        my $linecount = int((scalar(@values) +5)/ 6); #Rounding up.
        use ($linecount -1) instead of the hard coded 5.

        You subtract one because the starting value is 0.

                     My goal ... to kill off the slow brain cells that are holding me back from synergizing my knowledge of vertically integrated mobile platforms in local cloud-based content management system datafication.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1041289]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2014-09-16 01:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (155 votes), past polls