The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Append string to columns from 2 files karthikn7974 Shell Programming and Scripting 3 04-28-2008 06:32 AM
awk printing all columns after (but including) $n cassj UNIX for Dummies Questions & Answers 2 03-05-2008 12:22 PM
Need help in AWK;Search String and rearrange columns spring_buck Shell Programming and Scripting 2 04-05-2007 08:40 AM
Printing Columns in Unix Terrible Shell Programming and Scripting 3 11-27-2006 03:43 AM
printing columns cdunavent Shell Programming and Scripting 3 06-07-2003 08:31 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 05-19-2008
Registered User
 

Join Date: Apr 2008
Posts: 25
Perl, printing a string into columns

How can I use Perl to a take a string of 10 characters and print the last five characters of the string in columns 1-5 and the first five in columns 6-10?

Result:
0123456789

5 0
6 1
7 2
8 3
9 4
Reply With Quote
Forum Sponsor
  #2  
Old 05-20-2008
Registered User
 

Join Date: Oct 2004
Location: UK
Posts: 7
If the string is a constant and the output is also constant, use 'substr' to put each character into variables then print out in any format you want.

Code:
my $first_char = substr $_, 0, 1;
my $second_char = substr $_, 1, 1;
my $third_char = substr $_, 2, 1;
my $fourth_char = substr $_, 3, 1;
.
.
.
print "$sixth_char\t$first_char\n";
print "$seventh_char\t$second_char\n";
.
.
.
and so on
Reply With Quote
  #3  
Old 05-20-2008
agn agn is offline
Registered User
 

Join Date: Feb 2008
Posts: 97
Code:
$ echo 0123456789 | perl -nle 'for ($i=0; $i < 5; $i++) { print substr($_,$i+5,1)," ", substr($_,$i,1) }' 
5 0
6 1
7 2
8 3
9 4
Reply With Quote
  #4  
Old 05-20-2008
Registered User
 

Join Date: Apr 2008
Posts: 25
The string is actually going to be coming from STDIN.
Reply With Quote
  #5  
Old 05-21-2008
Registered User
 

Join Date: Apr 2008
Posts: 25
So I dug around and found some code similar to what I want, but not quite (similar in that it can break a string down by characters).

#!/usr/bin/perl
print "Enter 10 characters\n";
chomp ($foo=<STDIN>);
{
%seen = ();
foreach $char (split //, $foo) {
$seen{$char}++;
}
print "broken into columns: ", sort(keys %seen), "\n";
}
print $foo;

How can I get this to print characters into columns then?
Reply With Quote
  #6  
Old 05-21-2008
Registered User
 

Join Date: Apr 2008
Posts: 25
Nevermind. Got it.

#!/usr/bin/perl
print "Enter 10 characters\n";
chomp ($foo=<STDIN>);
@arr=split //, $foo;
for($i=0;$i<5;$i++) {
print $arr[$i+5] . " " . $arr[$i] . "\n";
}
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 12:34 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0