Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn an array of words into an array containing the characters of the words in order?

Tags:

string

perl

I have an array of unknown number of words, with an unknown max length. I need to convert it to another array, basically turning it into a column word array. so with this original array:

@original_array = ("first", "base", "Thelongest12", "justAWORD4");

The resluting array would be:

@result_array = ("fbTj","iahu","rses","selt","t oA","  nW","  gO","  eR","  sD","  t4","  1 ","  2 ");

Actually I will have this:

fbTj
iahu
rses
selt
t oA
  nW
  gO
  eR
  sD
  t4
  1
  2

I need to do it in order to make a table, and these words are the table's headers. I hope I have made myself clear, and appreciate the help you are willing to give.

I tried it with the split function, but I keep messing it up...

EDIT: Hi all, thanks for all the tips and suggestions! I learned quite much from all of you hence the upvote. However I found tchrist's answer more convenient, maybe because I come from a c,c# background... :)

like image 583
Yarok Avatar asked Aug 28 '11 12:08

Yarok


People also ask

How do you split words into an array?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.

How do you create an array of characters?

“array_name = new char[array_length]” is the syntax to be followed. Anyways we can do both declaration and initialization in a single by using “char array_name[] = new char[array_length]” syntax. The length of the array should be declared at the time of initialization in a char array.

How do you turn an array of letters into a string?

Another way to convert a character array to a string is to use the valueOf() method present in the String class. This method inherently converts the character array to a format where the entire value of the characters present in the array is displayed.

How do you store each word in an array?

To store the words, a 2-D char array is required. In this 2-D array, each row will contain a word each. Hence the rows will denote the index number of the words and the column number will denote the particular character in that word.


2 Answers

use strict;
use warnings;
use 5.010;
use Algorithm::Loops 'MapCarU';

my @original_array = ("first", "base", "Thelongest12", "justAWORD4");
my @result_array = MapCarU { join '', map $_//' ', @_ } map [split //], @original_array;
like image 164
ysth Avatar answered Sep 24 '22 14:09

ysth


I have an old program that does this. Maybe you can adapt it:

$ cat /tmp/a
first
base
Thelongest12
justAWORD4

$ rot90 /tmp/a
fbTj
iahu
rses
selt
t oA
  nW
  gO
  eR
  sD
  t4
  1 
  2 

Here’s the source:

$ cat ~/scripts/rot90
#!/usr/bin/perl 
# rot90 - [email protected]

$/ = '';

# uncomment for easier to read, but not reversible:
### @ARGV = map { "fmt -20 $_ |" } @ARGV;

while ( <> ) {
    chomp;
    @lines = split /\n/;
    $MAXCOLS = -1;
    for (@lines) { $MAXCOLS = length if $MAXCOLS < length; }
    @vlines = ( " " x @lines ) x $MAXCOLS;
    for ( $row = 0; $row < @lines; $row++ ) {
        for ( $col = 0; $col < $MAXCOLS; $col++ ) {
            $char = ( length($lines[$row]) > $col  )
                    ? substr($lines[$row], $col, 1) 
                    : ' ';
            substr($vlines[$col], $row, 1) = $char;
        }
    }
    for (@vlines) {
        # uncomment for easier to read, but again not reversible
        ### s/(.)/$1 /g;
        print $_, "\n";
    }
    print "\n";
}
like image 20
tchrist Avatar answered Sep 26 '22 14:09

tchrist