The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 09:32 AM
awk printing all columns after (but including) $n cassj UNIX for Dummies Questions & Answers 2 03-05-2008 03:22 PM
Need help in AWK;Search String and rearrange columns spring_buck Shell Programming and Scripting 2 04-05-2007 11:40 AM
Printing Columns in Unix Terrible Shell Programming and Scripting 3 11-27-2006 06:43 AM
printing columns cdunavent Shell Programming and Scripting 3 06-07-2003 11:31 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-19-2008
doubleminus doubleminus is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 27
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
  #2 (permalink)  
Old 05-20-2008
Simbad Simbad is offline
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
  #3 (permalink)  
Old 05-20-2008
agn agn is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 172
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
  #4 (permalink)  
Old 05-20-2008
doubleminus doubleminus is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 27
The string is actually going to be coming from STDIN.
  #5 (permalink)  
Old 05-21-2008
doubleminus doubleminus is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 27
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?
  #6 (permalink)  
Old 05-21-2008
doubleminus doubleminus is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 27
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";
}
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:04 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0