Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

fetching array of array, using a loop

by cool (Scribe)
on Jun 26, 2007 at 14:29 UTC ( #623415=perlquestion: print w/replies, xml ) Need Help??

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

Hi monks!

Problem:: I want values that I am pushing in the end with each input line (6,7....) to fetch as a loop ie it would be somewhat like given below as'desired output'. Now, I am not able to find the way so that I can get them easily. This output I am going to use for GD.pm.

Code summary- Input lines : 3-8 Subroutine : 22-68 {playing with data 33-60}# So these can be left for this + problem Printing line nos : 64 and 18

From line no 61-65 I am pushing my data into last position of each input line(line no 6,7 and 8). These pushed values in a set of 4, can be any no of times in the last place of each input line.

Pardon me if its very silly to put here. Further, pl feel free (Don't go all PerlMonks on me) to go all perl monk on me. Pl read roboticus 's second reply from Extra iteration while fetching 'fetchrow_array' in DBI and standerd way of using DBI.(I couldn't figure out how to give link to a particular reply)

Desired output G6081 yaaX 5234 5530 + 6 6 244 480 540 480 EG10011 yaaA 5683 6459 - 6 7 693 480 1010 480 10 560 469 560 EG11555 yaaJ 6529 7959 - 7 8 539 560 1010 560 10 640 969 640
My code:
1 #! /usr/bin/perl -w 2 use strict; 3 my $base_length=1000; 4 my $margin=10; 5 my @midfield; 6 $midfield[0]='G6081 yaaX 5234 5530 + 6 6'; 7 $midfield[1]='EG10011 yaaA 5683 6459 - 6 7'; 8 $midfield[2]='EG11555 yaaJ 6529 7959 - 7 8'; 9 my $mid_r=\@midfield; 10 11 my $cordinate_r=genedraw($mid_r,$base_length,$margin); 12 #print "$cordinate_r->[7][0]\n"; 13 my @refer=@{$cordinate_r}; 14 #my @tempref=@{$cordinate_r->[7]}; 15 #my @tempref; 16 foreach (@refer){ 17 18 print "@{$refer[7]}\n",'&&&&&&&&&&&&&&&&&&&&',"\n"; ## + PRINT-2 19 # push (@tempref,@{$refer[7]}) 20 21 } 22 sub genedraw 23 { 24 my $arr=shift; 25 my $Blength=shift; 26 my $margin=shift; 27 my @finalarr; 28 my @field=@{$arr}; 29 foreach (@field) 30 { 31 my $X2; 32 my $Y2; 33 my @tmp=split(/\s+/);#### LINE A 34 my @temparray; 35 my$X1=($tmp[2]-(($tmp[5]-1)*$Blength))+$margin +; 36 my$Y1=80*$tmp[5]; 37 my$check=1; 38 if($tmp[5] !=$tmp[6]) 39 { 40 my$genestretchlines=$tmp[6]-$tmp[5]; 41 for my $x(1..$genestretchlines) 42 { 43 if($check==1) 44 { 45 $X2=$margin+$Blength; 46 $Y2=80*$tmp[5]; 47 $check=2; 48 } 49 push @temparray, ($X1,$Y1,$X2, +$Y2); 50 if($x!=$genestretchlines) 51 { 52 $X2=$margin+$Blength; 53 $Y2=80*($tmp[5]+$x); 54 } 55 $X1=$margin; 56 $Y1=80*($tmp[5]+$x); 57 } 58 } 59 $X2=($tmp[3]-(($tmp[6]-1)*$Blength))+$margin; 60 $Y2=80*$tmp[6]; ##### LINE B 61 push @temparray, ($X1,$Y1,$X2,$Y2); 62 my$temp_r=\@temparray; 63 $tmp[7]=$temp_r; 64 print "\n@{$tmp[7]}:::TEMP\n"; ## PRINT-1 65 push @finalarr,@tmp; 66 } 67 return (\@finalarr); 68 }

Replies are listed 'Best First'.
Re: fetching array of array, using a loop
by shmem (Chancellor) on Jun 26, 2007 at 23:05 UTC
    Unclear spec:
    These pushed values in a set of 4, can be any no of times in the last place of each input line.

    But in your "Desired output" block there's only one set of four on a line? Then there's a line break, and the next line contains the continuation? Is that really what you want, or do you want the elements at index 7 in each array ref on one line? Then, you have those coordinate blocks neatly left-aligned at column 34. Is that a requirement? I'll assume it is.

    At line 65, put brackets around @tmp, otherwise you will have a flat list in @finalarr.

    From line 11 on things are getting messy. No need to dereference $cordinate_r into @refer, so no need for line 13.

    To get at the data:

    foreach my $ref(@$cordinate_r) { my $line = join ' ', @$ref[0..6]; # splice field 7 into groups of four my @ary = @{$ref->[7]}; my @coord; while(@ary) { push @coord, join ' ', splice @ary, 0, 4 ; } no warnings 'uninitialized'; # see 'perldoc perlform' for what follows write; format STDOUT = @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<< $line, shift @coord ~@<<<<<<<<<<<<<<<<<<<<<<<< shift @coord . }

    Output:

    G6081 yaaX 5234 5530 + 6 6 244 480 540 480 EG10011 yaaA 5683 6459 - 6 7 693 480 1010 480 10 560 469 560 EG11555 yaaJ 6529 7959 - 7 8 539 560 1010 560 10 640 969 640

    Be consistent with the alignment of your curlies!

    There's more to 'go all perlmonks on you', no time now... look for an update of this node.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      Dear shmem,

      First of all, thank you for your time. Yes, you are right.

      or do you want the elements at index 7 in each array ref on one line?

      I want @temparray to be stored at 7th index.

      Now after putting my @tmp in brackets; and using your lines of code I am getting this error. Can't use string ("G6081") as an ARRAY ref while "strict refs" in use at perly.pl line 24.

      Further, Is there anyway to know how @finalarr (line 89) is arranging its all elements down to bottom?? Pl help
        I suggst you get and install Data::Dump::Streamer or Data::Dumper. With either of those you can dump your data structure and examine it, e.g.
        use Data::Dump::Streamer; ... my $cordinate_r = genedraw ($mid_r,$base_length,$margin); Dump ($cordinate_r);

        The question about the array at the 7th index was about wether it is required to have its elements in packs of four on different lines, or can those just be lumped together on one line?

        Can't use string ("G6081") as an ARRAY ref while "strict refs" in use at perly.pl line 24.

        Is that the line after the foreach loop control statement, dereferencing $ref? Seems like you are not constructing the references properly, since the incriminated token is what should be $ref->[0] inside the foreach loop, not $cordinate_r->[0]. Anyways, here is your running script, slightly reformatted to make it readable.

        --shmem

        _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                      /\_¯/(q    /
        ----------------------------  \__(m.====·.(_("always off the crowd"))."·
        ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2020-02-25 19:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?










    Results (113 votes). Check out past polls.

    Notices?