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:
trying to parse string w/o regular expression

 



mixelplik
New User

May 6, 2014, 2:10 PM

Post #1 of 6 (3428 views)
trying to parse string w/o regular expression Can't Post

I'm trying to get a user to enter a sequence characters:
example: 12 3 f g 4
through <STDIN>

I want to be able to display the output 12 + 3 +4 = 12

I want to do this without a regular expression or an array.

I'm honestly stuck and get for the life of me figure out a solution. The comparison operators in perl seem to be so alien to me, (I have a c++ background)

I can get it to say 1+2+3+4 = 10 - I just can't lump numbers together everything keeps turing to mother.....er...Fing 0.

Can somone please help? Give a suggestion in which direction to head?

Code
$input = <STDIN>; 
chomp($input);
$length = length($input);
$total = 0;
$temp = 0;
for($i = 0; $i < $length; $i++)
{
if(substr($in, $i, 1) == substr($in, $i, 1) + 0)
{
$temp . substr($in, $i, 1);
print $total, "\n";
print "shit";
}
else
{
$total += $temp;
$temp = 0;
}
}


This is just a sketch now, as I have been writing and deleting forever and am finally turning to the BBs


FishMonger
Veteran / Moderator

May 6, 2014, 2:40 PM

Post #2 of 6 (3412 views)
Re: [mixelplik] trying to parse string w/o regular expression [In reply to] Can't Post

Why don't you want to use a regex or an array?


mixelplik
New User

May 6, 2014, 2:44 PM

Post #3 of 6 (3409 views)
Re: [FishMonger] trying to parse string w/o regular expression [In reply to] Can't Post

I'm trying to get a feel for how perl does things, using regex and arrays are pretty simple. I'm getting confused on what you can do with numbers as opposed to chars or strings. The fact that you have two sets of comparison operators, one for strings and one for numbers. I'm brand new to this language, and while it's pretty neat, it's also pretty weird, at least right it just feels weird and really different.


BillKSmith
Veteran

May 6, 2014, 9:41 PM

Post #4 of 6 (3257 views)
Re: [mixelplik] trying to parse string w/o regular expression [In reply to] Can't Post

You will never get it quite right until you specify the grammer of your input line.

It probably is not exactly what you want, but the example below works for the following input grammer.

The input consists of a string of ASCII characters terminated by a newline.

Any character is allowed, but characters other than digits and spaces are ignored.

A sequence of consecutive integers separated by one or more spaces is considered a single integer number.


The sum of the integer numbers in the string is displayed.


Code
use strict; 
use warnings;
print "Enter test string: ";
chomp( my $in = <STDIN> );
my $total = 0;
my $cur_num = 0;
my $last_index = length($in) - 1;
for my $i (0 .. $last_index) {
my $cur_char = substr $in, $i, 1;
next if !grep {$cur_char eq $_} (0 .. 9,' ');
if ( $cur_char eq ' ' ){
$total += $cur_num;
$cur_num = 0;
next;
}
$cur_num = 10 * $cur_num + $cur_char;
}
$total += $cur_num;
print $total, "\n";


UPDATE: Add a more "perlish" solution.

Code
use strict; 
use warnings;
use IO::Prompt::Hooked;
use Regexp::Common qw/number/;
use List::Util qw /sum/;
my $in = prompt("Enter string: ");
$in =~ s/[^0-9 ]//;
print sum( $in =~ /($RE{num}{int})/g ), "\n";

Good Luck,
Bill

(This post was edited by BillKSmith on May 7, 2014, 6:48 AM)


FishMonger
Veteran / Moderator

May 7, 2014, 6:53 AM

Post #5 of 6 (3069 views)
Re: [mixelplik] trying to parse string w/o regular expression [In reply to] Can't Post


Quote
I'm trying to get a user to enter a sequence characters:
example: 12 3 f g 4
through <STDIN>

I want to be able to display the output 12 + 3 +4 = 12


Why would you output the sum equaled 12 when in fact it equals 19?

Bill's solution is close to my first idea which made use of the ord() function, but since you didn't rule out modules, I decided to use a shorter, cleaner and easier to maintain solution.


Code
#!/usr/bin/perl 

use Modern::Perl '2010';
use Scalar::Util qw(looks_like_number);

say "Please enter your string";
chomp(my $str = <STDIN>);

my $total = 0;
for my $value (split ' ', $str) {
$total += $value if looks_like_number($value);
}
say "Total: $total";



BillKSmith
Veteran

May 7, 2014, 1:00 PM

Post #6 of 6 (3034 views)
Re: [mixelplik] trying to parse string w/o regular expression [In reply to] Can't Post

In reply to your question about numbers and strings.

Numbers have a default order, the numeric order of their value. Strings also have a default order, the order that they would appear in a dictionary. In perl, both strings and numbers are treated as scalars. Perl would not know which order to use if we did not specify. We do this by our choice of operators.

The problem does not arise in "C/C++" because strings are treated as arrays. The scalar operators always use numeric order. Note that you do have the same problem with the "C" functions that operate on strings.

Back to perl. If you try to use the numeric operators on scalars that contain anything but a single number, perl attempts to convert them to numbers. (This is seldom, if ever, what you intend. It is not worth thinking about.) If you use the string operators on a scalars that contain only a single number, perl treats each as string of digits. (This may be what you want, but probably not. e.g. (3 gt 12) is true )
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