parsing character by character and printing

I am completely new to Perl.

I am trying to figure out how I can parse a row, character by character,
that I have retrieved from a database.   I would like to format output
based on characters from the row.  For example I may have retrieved 250
character string from a database and I want to print that character
string, BUT I want to print only 50 characters a line or less.   How
might I do this if I have a variable called @row??


0
Brian
6/18/2001 5:43:16 PM
perl.beginners.cgi 3839 articles. 0 followers. Follow

3 Replies
461 Views

Similar Articles

[PageSpeed] 37

>I am trying to figure out how I can parse a row, character by 
>character, that I have retrieved from a database. 

You could try something like this:

  while ($row) { # while there are still characters left to look at
    ($char) = $row =~ s/(\w)//; # Removes the first word character of
                                # $row and puts it in $char

This will destroy the contents of $row, so you might want to make a copy of
it first if you're going to need it later.

>I would like to format output based on characters from the row.  For 
>example I may have retrieved 250 character string from a database and 
>I want to print that character string, BUT I want to print only 50 
>characters a line or less.   How might I do this if I have a variable 
>called @row??

Hopefully you mean a variable called $row. @row would be an array (a list of
(possibly) many variables). Anyway, building on above:

  while ($row) {
    for (1..50) { # Repeat 50 times
      ($char) = $row =~ s/(\w)//;
	$output .= $char; # Add $char to $output
    }
    $output .= "\n"; # Add a newline to $output
  }

This will probably produce lots of warnings if $row doesn't have a multiple
of 50 characters in it (you are using the -w switch I hope). But it should
work more or less.

Cheers

Mark C
-- 
------------------------------------------------------------------------
Dr Mark Crowe				John Innes Centre
						Norwich Research Park
Tel/Fax: +44 (1603) 450012		Colney		
mailto:Mark.Crowe@bbsrc.ac.uk		Norwich
						NR4 7UH

0
mark
6/19/2001 9:33:33 AM
In general, I would urge you to read the built-in functions for strings and 
scalars in the Perl documentation.

That said, an easy way to get the first fifty characters of a string (ie a 
substring of a string) use
my $substring = substr($row, 0, 50); # $row is the string, 0 is the 
starting point, 50 is the amount of characters we want to get out

This has the advantage of not affecting $row if you want to use it 
later.  Also, if $row is shorter than fifty characters, you'll get $row back.

You mention having a variable @row.  That would be an array, which, unlike 
C, is not a string. If a variable called 'row' should contain a string 
representing one row in a database, then you should refer to it as a 
scalar, ie $row.

Not sure if this is exactly what you want but I hope it points you in the 
right direction.

At 13:43 18.06.2001 -0400, Brian Jackson wrote:
>I am completely new to Perl.
>
>I am trying to figure out how I can parse a row, character by character,
>that I have retrieved from a database.   I would like to format output
>based on characters from the row.  For example I may have retrieved 250
>character string from a database and I want to print that character
>string, BUT I want to print only 50 characters a line or less.   How
>might I do this if I have a variable called @row??

Aaron Craig
Programming
iSoftitler.com

0
aaron
6/19/2001 10:41:51 AM
In general, I would urge you to read the built-in functions for strings and 
scalars in the Perl documentation.

That said, an easy way to get the first fifty characters of a string (ie a 
substring of a string) use
my $substring = substr($row, 0, 50); # $row is the string, 0 is the 
starting point, 50 is the amount of characters we want to get out

This has the advantage of not affecting $row if you want to use it 
later.  Also, if $row is shorter than fifty characters, you'll get $row back.

You mention having a variable @row.  That would be an array, which, unlike 
C, is not a string. If a variable called 'row' should contain a string 
representing one row in a database, then you should refer to it as a 
scalar, ie $row.

Not sure if this is exactly what you want but I hope it points you in the 
right direction.

At 13:43 18.06.2001 -0400, Brian Jackson wrote:
>I am completely new to Perl.
>
>I am trying to figure out how I can parse a row, character by character,
>that I have retrieved from a database.   I would like to format output
>based on characters from the row.  For example I may have retrieved 250
>character string from a database and I want to print that character
>string, BUT I want to print only 50 characters a line or less.   How
>might I do this if I have a variable called @row??

Aaron Craig
Programming
iSoftitler.com

0
aaron
6/19/2001 3:43:30 PM
Reply: