Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: How to store multiples lines/records in a array

by NetWallah (Abbot)
on Jun 28, 2013 at 18:00 UTC ( #1041305=note: print w/ replies, xml ) Need Help??


in reply to How to store multiples lines/records in a array

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


Comment on Re: How to store multiples lines/records in a array
Download Code
Re^2: How to store multiples lines/records in a array
by Perlseeker_1 (Acolyte) on Jul 02, 2013 at 14:35 UTC
    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: note [id://1041305]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (12)
As of 2014-07-30 12:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (231 votes), past polls