Showing posts with label unshift. Show all posts
Showing posts with label unshift. Show all posts

Wednesday, August 27, 2008

The Upside Down Of HTML On Unix and Linux: Backward

Hey there,

If today's title throws you off a little, that's good. I was going for an allegorical representation of the output of today's Perl script in the title of this post. The beauty of it is, even if I don't succeed, I have done my job, since (although this script is supposed to produce upside down and backwards HTML pages) the outcome of any input handed off to today's script may, or may not, have every single line reversed and in top to bottom order. In fact, when it comes to the more complicated issue of handling tags that dictate image, span, table, style and other such extravagant beautification, it may turn out a web page so horribly disfigured that you'll hardly recognize it. ...although, maybe that was the point. I allow my friends to talk me into wasting my time on folly like this far too often. ...probably because I enjoy it ;)

If you recall, or haven't checked it out yet, we put out a much more predictable script to reverse all the text in an HTML page yesterday ...well, most of it anyway. Reversing HTML tags themselves produces empty space for the most part :)

But, here it is: My final "figurative gun to the head" script-for-hire this week. When you see how beautifully the code is rendered and how the subtle nuance of each line seamlessly blends with every other in an intricate weave of logical, if not poetic, symmetry, you'll agree that I must be completely full of @@@@ :) Today's script is pure hack-n-slash. If anything, it might be eligible for priority placement in the circular file ;)

I took a snapshot of the bottom of my blog (I don't want to infringe upon anyone else's copyright but my own ;) from before the script conversion, shown here:

Click below to experience this picture in it's original splendour:

The sane version of our HTML page

and then this one of the top of the page after running it through the script:

Click below to be taken to another page where a larger version of this indecipherable nonsense resides:

The confusing version of our HTML page

The aim was, of course, to grab everything between the "body" tags in an HTML file, reverse every line - writing from right to left - excluding HTML tags, while printing the lines within the "body" in order from last to first. It's very easy to run on any webpage you download, like so:

host # ./updown.pl webpage.htm
Upside Down File Saved As: updown.webpage.htm.htm


And then you can open that up in your web browser and be amazed. Of course, your mileage may vary. Sites like Google were they do heavy CSS or any page that relies primarily on pictures to represent text won't give you that same sick-in-the-pit-of-your-stomach feeling. In fact, interestingly enough, except for the fact that you lose the pictures, Google comes out of a run through this script looking almost exactly the same!

Hope you enjoy playing around with this as much as my friends enjoyed criticizing my work while they casually ignored their own ;) Just kidding. But, in all seriousness, again, your results will most definitely vary. Take this script with a grain of salt, an ounce of prevention, a pound of cure and a ton of prescription pain-killers ;)

Tomorrow, we'll get back to some meatier subject matter,

Cheers,


Creative Commons License


This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License

#!/usr/bin/perl


#
# updown.pl - Completely fudge your web page!
#
# 2008 - Mike Golvach - eggi@comcast.net
#
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#

if ( $#ARGV != 0 ) {
print "Usage: $0 yourwebpage.html\n";
exit(1);
}
$FILE=$ARGV[0];

if ( ! -f $FILE ) {
print "File $FILE does not exist! Exiting...\n";
exit(2);
}

$OUTFILE="updown.${FILE}.htm";
$muck_it_up = 0;
open(FILE, "<$FILE");
@file=<FILE>;
close(FILE);

open(OUTFILE, ">$OUTFILE");
foreach $line (@file) {
if ( $line =~ /<BODY[^>]*>/i ) {
$muck_it_up = 1;
print OUTFILE "$line";
} elsif ( $line =~ /<\/BODY>/i ) {
$muck_it_up = 0;
foreach $ud (@upside_down) {
print OUTFILE "$ud\n";
}
print OUTFILE "$line";
} elsif ( $muck_it_up == 1 ) {
$tag = $line;
if ( $tag =~ /</ ) {
if ( $tag !~ /<(!|img|br)/ ) {
$tag =~ s/<\//<zOiKs/g;
if ( $tag !~ /zOiKs/ ) {
$tag =~ s/</<\//g;
}
$tag =~ s/zOiKs//g;
$tag =~ s/(<(h[12345]|span|a |br|p|div|textarea)[^>]*>)/$1<bdo dir="rtl">/ig;
$tag =~ s/(<(\/h[12345]|span|a |br|p|div|textarea)>)/<\/bdo>$1/ig;
}
}
unshift(@upside_down, $tag);
} else {
$line =~ s/(<(h[12345]|span|a |br|p|div|textarea)[^>]*>)/$1<bdo dir="rtl">/ig;
$line =~ s/(<(\/h[12345]|span|a |br|p|div|textarea)>)/<\/bdo>$1/ig;
print OUTFILE $line;
}
}
close(OUTFILE);
print "\nUpside Down File Saved As: $OUTFILE\n";
exit(0);


, Mike


Banner for College Student-Oriented Sites (728 version 1)



Please note that this blog accepts comments via email only. See our Mission And Policy Statement for further details.

Sunday, May 4, 2008

Reversing All Lines In A File On Linux Or Unix Using Perl

Good evening-day-morning-afternoon :)

In response to some positive feedback, and some additional questions, prompted by our earlier post on using Perl to mirror lines in a file on Linux or Unix, today we're dispensing with yet another Perl script to do almost the same thing, but with an extra twist. While our original mirror file script reversed each line, this one will attempt to do the same thing while also reversing the order of the lines. If you have a nervous condition you should probably quit reading this is, as it is only going to get more intense ;)

The good news is that I already know it works. The bad news is available at your local newsstand or in that meeting you should really try to blow off ;) Sorry... but only because the bad jokes are intentional.

So, when we're done crunching a file with today's script it will come out with all the lines reading from left to right and with the first line being the last, the second line being the second to last, and so on until the last line, which will be the first. End of story... or is it? ;)

Again, we're going to take a quick look at another useful function in this script. Since the rest of them are the same as in the script that prompted this one (split, join, undef and push), I should refer you to that post on mirroring file lines so that we don't waste too much space with duplicate content. It's interesting to look at the two side-by-side to really see the difference.

In today's post we'll check out the use of this one function (Note that, this time, we'll assume that the array @array consists of no members at all:

unshift - This function does the same thing as the "push" function we used in our last post, except that it will "unshift" a variable on to the "right side" of an array. The name isn't as intuitive as "push," but, if it helps, when you "shift" a variable from an array, you're pulling it out of the "left side." So, naturally, "unshifting" is adding to the "right side." ...No matter how I explain it, it will never make sense. It's just one of those words you have to accept on faith. Like "defenestration." <--- Apologies for the cheap-shot at Microsoft. That word is officially defined as "the act of throwing someone or something out of a window." It's only a few small steps to the jab I was going for ;)

Ex:
unshift(@array, "a");
<--- @array now equals "a"
unshift(@array, "b"); <--- @array now equals "a" "b"
unshift(@array, "c"); <--- @array now equals "a" "b" "c"

Again, enjoy, and I hope this helps you out with whatever you're doing that it might help you out with ;)

Cheers,

SAMPLE RUN:

host # cat words|head -3;cat words|tail -3
<--- The beginning and end of the file we're going to "reverse"
Aarhus
Aaron
Ababa
Zulu
Zulus
Zurich
host # ./mirror.pl words
host # ls
. .. reverse.pl words words.reverse
<--- Our newly created file is called "words.reverse"
host # cat words.reverse|head -3;cat words.reverse|tail -3 <--- And, once again (since I tested this a few times already), the reversal of the file seems to have worked!
hciruZ
suluZ
uluZ
ababA
noraA
suhraA

host # wc -l words* <--- Just double checking here to make sure that the number of characters in our original file and the reverse file are exactly the same, which they should be.
45378 words
45378 words.reverse
90756 total



Creative Commons License


This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License

#!/usr/bin/perl

# reverse.pl - reverse each line, and its position in a file
#
# 2008 - Mike Golvach - eggi@comcast.net
#
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#

if ( $#ARGV != 0 ) {
print "Usage: $0 text_file\n";
exit(1);
}

$text_file=$ARGV[0];

open(TXT, "<$text_file");
@txt = <TXT>;
close(TXT);

foreach $ln (@txt) {
chomp($ln);
@ln = split(//, $ln);
$ln_len = @ln;
undef(@rev);
while ( $ln_len > 0 ) {
push(@rev, $ln[${ln_len}-1]);
$ln_len--;
}
$rev = join(/ /, @rev);
unshift(@reverse, "$rev");
}
open(RTXT, ">${text_file}.reverse");
foreach $backward (@reverse) {
print RTXT "$backward\n";
}
close(RTXT);
exit(0);


, Mike