Solved

How to seperate and print integers in PERL

Posted on 2010-09-02
21
276 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.
0
Comment
Question by:PMG76
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 4
  • 3
  • +2
21 Comments
 
LVL 25

Expert Comment

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

  perldoc -f substr
0
 

Author Comment

by:PMG76
ID: 33592616
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.,
0
 
LVL 84

Expert Comment

by:ozo
ID: 33592793
can you use the join and unpack functions?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

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

0
 
LVL 84

Expert Comment

by:ozo
ID: 33592814
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
0
 
LVL 10

Expert Comment

by:jeromee
ID: 33592826
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
0
 
LVL 84

Expert Comment

by:ozo
ID: 33592842
print "$1 " while s/(\d)//
#or
print join" ",/\d/g
0
 

Author Comment

by:PMG76
ID: 33592851
I can't use arrays
0
 
LVL 84

Expert Comment

by:ozo
ID: 33592878
does that mean you can't use @ARGV to get arguments from the command line?
does that mean you can't use lists?
0
 

Author Comment

by:PMG76
ID: 33592879
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;
0
 
LVL 84

Expert Comment

by:ozo
ID: 33592941
> I can't use arrays
do you need help to overcome a difficulty you are having with using arrays?
0
 
LVL 84

Expert Comment

by:ozo
ID: 33592980
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
0
 
LVL 84

Expert Comment

by:ozo
ID: 33592998
// by default operates on $_
you can either do
$num =~ /\d/g
or
$_ = <>;
(unless you are trying to avoid using @ARGV)
0
 
LVL 5

Expert Comment

by:vikaskhoria
ID: 33594001
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

0
 
LVL 84

Accepted Solution

by:
ozo earned 250 total points
ID: 33594183
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"
0
 
LVL 84

Expert Comment

by:ozo
ID: 33594184
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"
0
 

Author Comment

by:PMG76
ID: 33595642
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";

0
 
LVL 10

Assisted Solution

by:jeromee
jeromee earned 250 total points
ID: 33596523
#!/usr/bin/perl
use warnings;
use strict;
print "\n";
print 'Enter a five digit number: ' ;
my $num = <>;

$num =~ s/(\d)/$1   /g;    #<<<< add 3 spaces here
chop($num);

print "$num\n";
0
 
LVL 5

Expert Comment

by:vikaskhoria
ID: 33596773
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;

0
 
LVL 84

Expert Comment

by:ozo
ID: 33602276
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.
0
 
LVL 84

Expert Comment

by:ozo
ID: 33602279
#!/usr/bin/perl
use warnings;
use strict;
#another way
$/=\1;
$\=" "x3;
print while <>;
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Math Stumper 6 59
Functions 7 90
Regular expression pattern macth to weed out words with HTML character codes & entities 7 152
read an xml file in perl 2 83
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
This article provides a brief introduction to tissue engineering, the process by which organs can be grown artificially. It covers the problems with organ transplants, the tissue engineering process, and the current successes and problems of the tec…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

742 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question