CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Manipulating the printing of array elements

 



Peter
Novice

Mar 21, 2016, 1:24 PM

Post #1 of 11 (2699 views)
Manipulating the printing of array elements Can't Post

I know that it is possible to print out array elements one by using the pop function. However I am wondering if it possible to pop arbitrary amounts of elements at a time.


Chris Charley
User

Mar 21, 2016, 4:01 PM

Post #2 of 11 (2697 views)
Re: [Peter] Manipulating the printing of array elements [In reply to] Can't Post

See splice.

Code
my @array = 1..11; 

print join ", ", splice @array, -3;

# prints 9, 10, 11



Peter
Novice

Mar 22, 2016, 6:52 AM

Post #3 of 11 (2684 views)
Re: [Chris Charley] Manipulating the printing of array elements [In reply to] Can't Post

It seems as though I cannot use arrays to my achieve goal.
I am trying to print 2 separate strings in pairs so that one string sits underneath another, followed by a new line, followed by another pair of the 2 strings until the entire string has been printed.

So far I have managed to use the unpack function to print one string with any amount of characters per line. But where I struggle is finding a way to print one line from one string followed by one line from another string, as follows:

(S1)AAAAAAAAAAAAABBBBBBBBBBBCCCCCCCCCCCCCCCCC
(S2)jkjkjkjkjkjkjkjkjkjkjkjkjkjjkjkjkjkjkjkjkjkjkjkjkjkjkjkjkjk
newline-
(S1)DDDDDDDDDEEEEEEEEEEEEEEEEEEFFFFFFFFFFFFFGGG
(S2)lmlmlmlmlmlmlmlmlmlmlmlmlmlmlmlmlmlmlmlmlml
newline-

I've also tried to go about it using the substring method within a loop, but after applying the substr method once within the loop, the second iteration is applied to an amended string, not the original string so that doesn't work.

Anyway here's my code for the unpack function:

Code
 
my $string = "SIHDKJHSFKJSDSFHFKSHKFJSHKLJFHSJKFHSKJFHSKJFHSFKJHSKFJHS";
my $string2 = "AAAAAAAAAAAAAAFFFFFFFFFFFFFFFFGGGGGGGGGGGGGGGGGGGJJJJ;

print "$_\n" for unpack '(A5)*', $string;


Laurent_R
Veteran / Moderator

Mar 22, 2016, 12:42 PM

Post #4 of 11 (2676 views)
Re: [Peter] Manipulating the printing of array elements [In reply to] Can't Post

Hm, you should probably not use unpack for that.

The substr function will be more malleable, but please show your code to figure out how it can be fixed.


Peter
Novice

Mar 22, 2016, 1:42 PM

Post #5 of 11 (2674 views)
Re: [Laurent_R] Manipulating the printing of array elements [In reply to] Can't Post

I have something along these lines:


Code
 
my $string1 = join ('', 'A' .. 'J') x 25;
my $string2 = join (' ', 'v' .. 'z') x 25;

my $start =0;
my $end =50;

my $new_string1;
my $new_string2;


for (my $i = 0; $i<length($string1); $i++){
if (length($string1) > 0){
my $new_string1 = substr ($string1, $start, $end);
print "$new_string1 \n";
$start = $start +50;
$end = $end +50;
}
for (my $j = 0; $j<length($string2); $j++){
if (length($string2) > 0){
my $new_string2 = substr ($string2, $start, $end);
print "$new_string2\n";
$start = $start +50;
$end = $end +50;
}
}
}


BillKSmith
Veteran

Mar 22, 2016, 4:37 PM

Post #6 of 11 (2669 views)
Re: [Peter] Manipulating the printing of array elements [In reply to] Can't Post

I think that this is what you want:

Code
use strict; 
use warnings;
my $string1 = 'ABCDEFGHIJ' x 25;
my $string2 = ' V W X Y Z' x 25;
my $offset = 0;
my $length = 50;

while ($offset < length($string1)) {
print substr( $string1, $offset, $length), "\n";
print substr( $string2, $offset, $length), "\n";
print "\n";
$offset += $length;
}


Output:

Code
C:\Users\Bill\perl\guru>peter_2.pl 
ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJ
V W X Y Z V W X Y Z V W X Y Z V W X Y Z V W X Y Z

ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJ
V W X Y Z V W X Y Z V W X Y Z V W X Y Z V W X Y Z

ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJ
V W X Y Z V W X Y Z V W X Y Z V W X Y Z V W X Y Z

ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJ
V W X Y Z V W X Y Z V W X Y Z V W X Y Z V W X Y Z

ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJ
V W X Y Z V W X Y Z V W X Y Z V W X Y Z V W X Y Z


Note:

No arrays are used in this solution.

I borrowed the variable names $offset and $length from the documentation for substr in perlfunc.
Good Luck,
Bill

(This post was edited by BillKSmith on Mar 22, 2016, 7:28 PM)


Laurent_R
Veteran / Moderator

Mar 23, 2016, 12:38 AM

Post #7 of 11 (2661 views)
Re: [Peter] Manipulating the printing of array elements [In reply to] Can't Post

Bill has given you a perfect and simple solution.

The problem with your code:

Code
	if (length($string1) > 0){  
my $new_string1 = substr ($string1, $start, $end);
print "$new_string1 \n";
$start = $start +50;
$end = $end +50;
}


is that it seems the you did not make a decision about the approach.

One approach is to leave the original string untouched and just modify the offset to move forward within the string. (That's the esssence of Bill's suggested solution.

The other possible approach is to remove from the original string the parts that you have already processed. This approach is probably a bit more difficult to get it right and is less efficient (which matters only of your strings are massive).

But you can't do both at the same time.


Peter
Novice

Mar 24, 2016, 11:01 AM

Post #8 of 11 (2648 views)
Re: [Laurent_R] Manipulating the printing of array elements [In reply to] Can't Post

Thanks Bill.
Laurent what you've said makes a lot of sense when you put it that way.
But now that I think of it I can see it being problematic in printing my results through a CGI script.
This is somewhere I seem to be getting a lot of trouble with in terms of where to place my <p> tags.
If placed at either end of what follows the print function it interferes with the purpose of the loop, because it renders
the substr function inactive and treats it like text.
How do I get it to be treated like a function?


BillKSmith
Veteran

Mar 24, 2016, 12:10 PM

Post #9 of 11 (2644 views)
Re: [Peter] Manipulating the printing of array elements [In reply to] Can't Post


Code
    print '<p>',substr( $string1, $offset, $length), '</p>', "\n";  
print '<p>, substr( $string2, $offset, $length), '</p>', "\n";
print "<p> </p>\n";


This will not look quite like the standard output. "<p>...</p>" is not the same as a newline. Your browser probably defaults to a proportional font.
Good Luck,
Bill

(This post was edited by BillKSmith on Mar 24, 2016, 2:47 PM)


Peter
Novice

Mar 24, 2016, 1:10 PM

Post #10 of 11 (2643 views)
Re: [BillKSmith] Manipulating the printing of array elements [In reply to] Can't Post

So in order to prevent the <p> conflicting with perl functions we use single quotes and a comma? I was scrawling the web to find this out.


BillKSmith
Veteran

Mar 24, 2016, 3:19 PM

Post #11 of 11 (2636 views)
Re: [Peter] Manipulating the printing of array elements [In reply to] Can't Post

Learn to use perl's documentation tool 'perldoc'. From your command line, type

Code
perldoc perldoc


Read the first line of the documentation of 'print' (From perlfunc):

Quote
print FILEHANDLE LIST
print FILEHANDLE
print LIST
print
Prints a string or a list of strings.

(Emphasis mine)

Read the first line of 'List value constructors' (From perldata):

Quote
List value constructors
List values are denoted by separating individual values by commas.........


Continue the same way to find out what a string is and what is the difference between a single and double quoted string.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives