Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > open a ascii file and rotate the content 90 deg...

Reply
Thread Tools

open a ascii file and rotate the content 90 deg...

 
 
lyoute
Guest
Posts: n/a
 
      02-02-2004
i got a set of files all looks like: (with fixed dimension 8x
..X..XO..
X...XO..
OXX.XO..
OOOXXO..
....OO.O.
...O.....
.........
.........

how can i produce output with 90 deg rotated:
..XOO....
X.XO....
...XO.O..
....XO...
XXXXO...
OOOO....
.....O...
.........

actually i got this done while i still storing the pattern in a
@pattern[8][8].
just doubt if i still can rotate it after the pattern was written into a
file....
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      02-02-2004
lyoute <> wrote:

> how can i produce output with 90 deg rotated:


> actually i got this done while i still storing the pattern in a
> @pattern[8][8].
> just doubt if i still can rotate it after the pattern was written into a
> file....



Read it from the file into an 8x8 matrix and rotate it the
way you are already doing it.

What problem do you see with that?


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
 
lyoute
Guest
Posts: n/a
 
      02-02-2004
i see. so there are ways to read a file char by char?

Tad McClellan wrote:

> lyoute <> wrote:
>
>
>>how can i produce output with 90 deg rotated:

>
>
>>actually i got this done while i still storing the pattern in a
>>@pattern[8][8].
>>just doubt if i still can rotate it after the pattern was written into a
>> file....

>
>
>
> Read it from the file into an 8x8 matrix and rotate it the
> way you are already doing it.
>
> What problem do you see with that?
>
>

 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      02-02-2004
lyoute <> writes:

> i got a set of files all looks like: (with fixed dimension 8x
> .X..XO..
> X...XO..
> OXX.XO..
> OOOXXO..
> ...OO.O.
> ..O.....
> ........
> ........
>
> how can i produce output with 90 deg rotated:
> .XOO....
> X.XO....
> ..XO.O..
> ...XO...
> XXXXO...
> OOOO....
> ....O...
> ........


That is not rotation, that is reflection.

#!/usr/bin/perl
use strict;
use warnings;
chomp ( my @data = <> );
my $count;
do {
$count = 0;
for ( @data ) {
if ( s/^(.)// ) {
print $1;
$count ++;
} else {
print ' ';
}
}
print "\n";
} while $count;

__END__

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
Reply With Quote
 
Tassilo v. Parseval
Guest
Posts: n/a
 
      02-02-2004
[ Please put your replies below the text you are replying to.
Chronology restored. ]

Also sprach lyoute:
> Tad McClellan wrote:
>
>> lyoute <> wrote:
>>
>>
>>>how can i produce output with 90 deg rotated:

>>
>>
>>>actually i got this done while i still storing the pattern in a
>>>@pattern[8][8].
>>>just doubt if i still can rotate it after the pattern was written into a
>>> file....

>>
>>
>>
>> Read it from the file into an 8x8 matrix and rotate it the
>> way you are already doing it.
>>
>> What problem do you see with that?


> i see. so there are ways to read a file char by char?


Yes, sure. You can use getc() for that. It's not used often in Perl
though since there are usually better ways to do it. I'd store the 8x8
picture in one scalar variable and process them character-wise. For
instance that way:

use constant NUM_COLS => 8;

my $pic = do { local $/; <DATA> };
$pic =~ tr/\n//d;

my @rot;
my $i = 0;
while (my $c = substr $pic, $i, 1) {
$rot[$i++ % NUM_COLS] .= $c;
}
my $rotated = join "\n", @rot;

__DATA__
.X..XO..
X...XO..
OXX.XO..
OOOXXO..
...OO.O.
..O.....
........
........

This works unchanged even for pictures with more (or less) than 8 rows.
I used NUM_COLS because that way you can easily make it work for input
pictures with a different number of columns.

There are variations on the above, such as using split() instead of
substr(). That's mostly a matter of personal preferences.

Tassilo
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus}) !JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexi ixesixeseg;y~\n~~dddd;eval
 
Reply With Quote
 
Matt Garrish
Guest
Posts: n/a
 
      02-02-2004

"lyoute" <> wrote in message
news:401e2e12$...
>
> i see. so there are ways to read a file char by char?
>


The following should at least give you a start:

my @array;

while (my $line = <DATA>) {

chomp($line);
die if length($line) > 8;
die if $. > 8;

my $i = $. - 1;

foreach my $char ($line =~ /(.)/g) {

push @{$array[$i]}, $char;

}

}


__DATA__
..X..XO..
X...XO..
OXX.XO..
OOOXXO..
....OO.O.
...O.....
.........
.........


 
Reply With Quote
 
kz
Guest
Posts: n/a
 
      02-02-2004
"lyoute" <> wrote in message
news:...
> i got a set of files all looks like: (with fixed dimension 8x

[snip]
> actually i got this done while i still storing the pattern in a
> @pattern[8][8].
> just doubt if i still can rotate it after the pattern was written into a
> file....


I guess the matrix approach is the most flexible thing, you can rotate,
mirror, flip things just playing with the indices. Comment/uncomment lines
at your ease and compare the results.
I illustrated it step-by-step but one might want to code it more
efficiently. Works for any NxN matrix.

HTH,

Zoltan

use strict;
use warnings;
my ($line,@arr);
my $ctr = 0;
while (my $line = <DATA>) {
chomp($line);
$arr[$ctr++] = $line; }

# no transformation
foreach my $xc (0..$#arr) {
foreach my $yc (0..$#arr) {
# print substr($arr[$xc],$yc,1); # no transformation
print substr($arr[$yc],$xc,1); # rotate 90 degrees
# print substr($arr[$xc],$#arr-$yc,1); # flip horizontally
# print substr($arr[$#arr-$xc],$yc,1); # flip vertically, etc
}
print "\n";
}

__DATA__
..X..XO..
X...XO..
OXX.XO..
OOOXXO..
....OO.O.
...O.....
.........
.........


 
Reply With Quote
 
Chris Mattern
Guest
Posts: n/a
 
      02-02-2004
Brian McCauley wrote:
> lyoute <> writes:
>
>
>>i got a set of files all looks like: (with fixed dimension 8x
>>.X..XO..
>>X...XO..
>>OXX.XO..
>>OOOXXO..
>>...OO.O.
>>..O.....
>>........
>>........
>>
>>how can i produce output with 90 deg rotated:
>>.XOO....
>>X.XO....
>>..XO.O..
>>...XO...
>>XXXXO...
>>OOOO....
>>....O...
>>........

>
>
> That is not rotation, that is reflection.
>


No, it's not even reflection. Danged if I can figure out *what*
it's supposed to be. Any rotation or reflection would move
those two Xs in the top left away, because there isn't anything
like them anywhere else in the matrix. Nowhere else is there

..X
X.

or

X.
..X

Chris Mattern

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      02-02-2004
On Mon, 2 Feb 2004, Chris Mattern wrote:

> Brian McCauley wrote:
> > lyoute <> writes:
> >
> >
> >>i got a set of files all looks like: (with fixed dimension 8x
> >>.X..XO..
> >>X...XO..
> >>OXX.XO..
> >>OOOXXO..
> >>...OO.O.
> >>..O.....
> >>........
> >>........
> >>
> >>how can i produce output with 90 deg rotated:
> >>.XOO....
> >>X.XO....
> >>..XO.O..
> >>...XO...
> >>XXXXO...
> >>OOOO....
> >>....O...
> >>........

> >
> >
> > That is not rotation, that is reflection.
> >

>
> No, it's not even reflection. Danged if I can figure out *what*
> it's supposed to be. Any rotation or reflection would move
> those two Xs in the top left away, because there isn't anything
> like them anywhere else in the matrix. Nowhere else is there
>
> .X
> X.
>
> or
>
> X.
> .X
>


It is reflection, but it's not reflection about the X or Y axis. Rather,
it's reflection about the line x=y, which is just odd...
 
Reply With Quote
 
Chris Mattern
Guest
Posts: n/a
 
      02-02-2004
Paul Lalli wrote:
> On Mon, 2 Feb 2004, Chris Mattern wrote:
>
>
>>Brian McCauley wrote:
>>
>>>lyoute <> writes:
>>>
>>>
>>>
>>>>i got a set of files all looks like: (with fixed dimension 8x
>>>>.X..XO..
>>>>X...XO..
>>>>OXX.XO..
>>>>OOOXXO..
>>>>...OO.O.
>>>>..O.....
>>>>........
>>>>........
>>>>
>>>>how can i produce output with 90 deg rotated:
>>>>.XOO....
>>>>X.XO....
>>>>..XO.O..
>>>>...XO...
>>>>XXXXO...
>>>>OOOO....
>>>>....O...
>>>>........
>>>
>>>
>>>That is not rotation, that is reflection.
>>>

>>
>>No, it's not even reflection. Danged if I can figure out *what*
>>it's supposed to be. Any rotation or reflection would move
>>those two Xs in the top left away, because there isn't anything
>>like them anywhere else in the matrix. Nowhere else is there
>>
>>.X
>>X.
>>
>>or
>>
>>X.
>>.X
>>

>
>
> It is reflection, but it's not reflection about the X or Y axis. Rather,
> it's reflection about the line x=y, which is just odd...


Yes, Brian pointed it out to me in email. I see it now. Actually,
with standard Cartesian coordinates (positive being up and right),
it's the line y = -x.

Chris Mattern

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
error: Only Content controls are allowed directly in a content page that contains Content controls. hazz ASP .Net 6 06-09-2010 01:54 PM
PIL rotate : Rotate By Shear / Paeth Rotation? IanJSparks Python 0 01-10-2008 04:50 PM
Regex with ASCII and non-ASCII chars TOXiC Python 5 01-31-2007 04:48 PM
Rotate the graphics without rotate the text in SVG RC XML 1 08-03-2006 07:45 AM
routine/module to translate microsoft extended ascii to plain ascii James O'Brien Perl Misc 3 03-05-2004 04:33 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57