We help IT Professionals succeed at work.

How to seperate and print integers in PERL

PMG76
PMG76 asked
on
310 Views
Last Modified: 2012-08-14
I need some help with an assignment.  I need to input a 5 digit integer and then print the 5 numbers to the screen with a space between them.  i need some help getting this set up.  i can't use arrays or the split function.
Comment
Watch Question

We can't do your homework for you.  Do you have anything started?  Take a look at the substr function:  

  perldoc -f substr

Author

Commented:
I'm not asking anyone to do it.  i clearly stated that it was homework.  From what i gather I need to use the chop and chomp functions but I'm unsure on how to start it.,
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
can you use the join and unpack functions?

Commented:
perl -e '$_=$ARGV[0]; while( /(\d)(\d*)/ ){ print "$1 "; $_=$2 }' 12345
1 2 3 4 5

ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
Do you know how to find documentation on the chop function?
perldoc -f chop
       chop VARIABLE
       chop( LIST )
       chop    Chops off the last character of a string and returns the
               character chopped.  It is much more efficient than "s/.$//s"
               because it neither scans nor copies the string.  If VARIABLE is
               omitted, chops $_.  If VARIABLE is a hash, it chops the hash's
               values, but not its keys.

               You can actually chop anything that's an lvalue, including an
               assignment.

               If you chop a list, each element is chopped.  Only the value of
               the last "chop" is returned.

               Note that "chop" returns the last character.  To return all but
               the last character, use "substr($string, 0, -1)".


You can do a loop printing from the end of the string

Commented:
if you don't want the trailing space:
perl -e '$_=$ARGV[0]; while( /(\d)(\d*)/ ){ print "$1";  print " " if $2; $_=$2 }' 12345
1 2 3 4 5
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
print "$1 " while s/(\d)//
#or
print join" ",/\d/g

Author

Commented:
I can't use arrays
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
does that mean you can't use @ARGV to get arguments from the command line?
does that mean you can't use lists?

Author

Commented:
This is where I am right now but it's clearly not working.


use warnings;
use strict;
print "\n";
print 'Enter a five digit number: ' ;
my $num = <>;
##chop $num;
##my $saveme = chop $num;
##print "$num   ";
##perl -e '$_=$ARGV[0]; while( /(\d)(\d*)/ ){ print "$1";  print "   " if $2; $_=$2 }' $num;
print join"    ",/\d/g;
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
> I can't use arrays
do you need help to overcome a difficulty you are having with using arrays?
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
the shift function can get a value from @ARGV without explicitly naming it,
but it still uses it implicitly.
<> also uses @ARGV implicitly.
You might avoid that by using <STDIN> instead
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
// by default operates on $_
you can either do
$num =~ /\d/g
or
$_ = <>;
(unless you are trying to avoid using @ARGV)
try this code, simply replace each digit with the digit + a space!


use warnings;
use strict;
print "\n";
print 'Enter a five digit number: ' ;
my $num = <>;

$num ~= s/\d/$1 /g;
print $num;


### Or you can also do this:
$num ~= s/\d/$1 /g;
$num ~= s/ $//;  ## To remove the last space added in previous line
or
chomp($num); ## To remove the last space added in previous line

print $num;

Open in new window

CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
you probably meant
$num =~ s/(\d)/$1 /g;
and
chop($num)

more simply
perl -pe "s// /g"
or to avoid extra space at the end
perl -pe "s/\B/ /g"

Author

Commented:
Ok.  i got it to print the numbers seperately now but i need them to print with 3 spaces between each number.  How do i accompkish this:

#!/usr/bin/perl
use warnings;
use strict;
print "\n";
print 'Enter a five digit number: ' ;
my $num = <>;

$num =~ s/(\d)/$1 /g;
chop($num);

print "$num   \n";

Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Yeah I am sorry about the chomp - its a chop.
Thanks OZO

PMG76 :

3 instead of one is simple, right??

Just replace one space with 3!! :)
Like this:

$num =~ s/(\d)/$1   /g;

or you can also do this, I guess:
$num =~ s/(\d)/$1 {3}/g;

ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
s/(\d)/$1 {3}/g;
does not work
you could do
s/(\d)/$1." "x3/eg;
or
s/(\d)/$1@{[" "x3]}/g;
but it seems like a silly way to do it.


$num =~ s/(\d)/$1   /g;    #<<<< add 3 spaces here
chop($num);
leaves two spaces at the end if $num originally ended with a digit,
or if it originally ended with  non-digit, it would remove that character.
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
#!/usr/bin/perl
use warnings;
use strict;
#another way
$/=\1;
$\=" "x3;
print while <>;

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.