Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Lady_Aleena

by Lady_Aleena (Curate)
on Apr 25, 2002 at 08:28 UTC ( #161890=user: print w/replies, xml ) Need Help??

I am Aleena, the Lady of the Gray, Saver of bytes, and the Lonely coder. White space is not my friend but a good ally. I will fight white space wherever I come across it in large amounts. A byte saved today can be used tomorrow in good cause. I am in a tower alone with nothing but silence surrounding me.

What I like about PerlMonks is the only expectation of me they have is I improve myself. To that end, PerlMonks go out of their way to give me the resources to do so. Also, I will more than likely be remembered most for the cookies I give out. :)

Over the years I have been here, most made me feel welcome here, but a few have made it plain my presence is very unwelcome. I hope to not push away those who have been friendly to me so far. I also hope that those who actively dislike me will soften their views after reading this, if they read it.

"I thought that once I get syntax OK, I would be out of the woods. That only makes the woods thicker when there is a problem." - by me

Socialize with me

Goal

My programming goal is to completely rewrite my entire web site using Perl. This task is daunting as I have so many different ways I have to manipulate the data. I have gotten most of it figured out, though there are areas with which I am not quite happy.

The past

Coming from the world of markup languages into the world of programming languages was extremely daunting for me and still is upon occasion. Not knowing the right questions to ask, the correct terminology, or even what to put into searches made me feel very, very anxious. I am interested in what can be done in Perl. Until I get a grip on those areas where I am weak, would you please be extremely patient with me?

Annoyance caused me to state when my currant project was finished, you would never hear from me again. My feelings got hurt and my project was at a complete standstill when my temper got the best of me (which it does quite often when my feelings are hurt).

I had to see perl in a new light to get my project into motion again. When I wrote my first working module, I really got things going.

Coding history

A little less than a decade ago, I got 10 megabytes of complimentary web space from my internet service provider along with several e-mail addresses. Wanting to put my mark on the net, I created my first web page in Microsoft Word. Everything was going fine. My site got bigger and bigger, and Word even gave me frames. It was when I wanted to put some code on my site from somewhere else when the need to know how to write HTML caught up with me. No matter what I did in Word, the block would not go where I wanted it. I got the message, learn how to write a web page. Before that moment I did not care about the markup, I just wanted my pages to look pretty.

At first I found a site here and there which told me what all those things meant in between the < and >. With just the basics, I started to rewrite my site. I kept the frames and still did a lot wrong. I had depreciated tags everywhere. The amount of <font> tags was enough to make some throw their hands up in the air in exasperation. Not only did I have a lot of depreciated tags, I had tags unclosed. I just plain did not get it.

It took some very patient people talking me through my hysterics when something on my site did not work to finally get my site looking better and most areas working right. Then I ran out of space. I had filled my 10 megabytes rather quickly. Hysterics ensued. That is when someone casually mentioned CSS. When I saw what one line of CSS could do to cut my page size down, I nearly cried (or did I actually feel a tear?). Within weeks the size of my site was down by more than 75%. After that my whole coding philosophy became small is beautiful.

Let's pause for a moment. At the time the only browser on my computer was Internet Explorer. I was still a very lazy writer, so some of my HTML was browser specific. I was not writing for the masses, just those who were using IE. Then along came Firefox becoming the cause for a fresh bought of hysterics. I still have a few cross browser issues, but they are not critical.

The main issue causing my headaches was my frames looked bad in Firefox. So, I had to find another way of making my pages look and act the same without frames. Then I was introduced to <divs>s and in CSS scrolling. The problem with getting rid of the frames was I would have to put the same stuff on all of the pages, making them bigger. I did not want that. Then I was introduced to Server Side Includes. It was a miracle to me. I could put all kinds of stuff into one file and have it read by another. So slowly and up until six years ago, I had been altering all of my pages to have SSI.

I was talking to several people here on PerlMonks. When I told them that I was using SSI to add markup to my pages, I was told Perl could do the same thing. I was wondering, would Perl be smaller than SSI; and the answer is a resounding YES!

The present

I still have a lot of hurdles to overcome before I get anywhere near good at Perl. I have been told that I have come a long way from when I started here; yet with the amount that I have left to learn, it seems that I am still at the beginning. I am getting better at reading the perldocs and finding modules that may work for me. I have also developed a few peeves which may or may not stick with me as I learn more.

Have a cookie for making it this far into my node! I am impressed with your resilience! I tend to babble when given the chance. Have another if you intend to continue reading. :)

Objects

I have read and searched and read again about them, but they still do not make sense to me. I would need to see them in action with some code that I have already written to maybe get a grip on them. There are no promises on that score; ikegami, GrandFather, and others have tried to show me without it sinking in how they work. Because of my frustration with the topic, I am beginning to dislike almost everything to do with objects, even eschewing object oriented modules. I would rather write my own function then wrestle with an object to make it behave. I wrote the following function because Text::CSV with its objects drove me wild.

# written with rindolf in #perlcafe on freenode; golfed with the help +of [GrandFather] of PerlMonks. # further help from [GotToBTru] (solved the 0 problem) sub get_hash { my %opt = @_; my $file = $opt{'file'} && ref($opt{'file'}) eq 'ARRAY' ? data_file( +@{$opt{'file'}}) : $opt{'file'}; open(my $fh, '<', $file) or die("can't open $file $!"); my %hash; while (my $line = <$fh>) { chomp $line; die "This file is not for Base::Data! Stopped" if $line =~ /no Bas +e::Data/i; my @headings = $opt{'headings'} ? @{$opt{'headings'}} : ('heading' +); my @values = split(/\|/,$line); my $key = scalar @headings > 1 ? $values[0] : shift @values; my $n = 0; for my $r_heading (@headings) { goto INC if (!defined($values[$n]) || length($values[$n]) == 0); my $split = $r_heading =~ /\+$/ ? 1 : 0; (my $heading = $r_heading) =~ s/\+$//; my $value = defined($values[$n]) ? $split == 1 ? [map { $_ =~ s/ +^ //; $_ } split(/;/,$values[$n])] : $values[$n] : undef; if (scalar @headings > 1) { $hash{$key}{$heading} = $value; } else { $hash{$key} = $value; } INC: ++$n; } } return \%hash; }

get_hash is so much easier for me to use than the alternatives offered on cpan, mostly because they are not written to do what I need them to do easily.

Update 1: I am giving them another try again. I hope this time that how to write them sinks in.

Update 2: They have confounded me again! I do not get it!

Update 3: I gave it yet another try, nothing. I still do not understand what is so wonderful about objects. So after Why won't a hash in a hash work as a hash reference to create an object?, RFC on how I'm doing when it comes to writing objects?, and How do I go from procedural to object oriented programming?; I give up. If one day my code morphs into objects on their own, I won't know it. To me objects are just a pain the a** especially since everyone tells me to separate my data out of them. Well, I can not do that. My data stays where it is with the subroutines which use it.

Over-complication

I have been told that I have a knack for this. There are times when reinventing the wheel is a good thing. For example, I was going to use DateTime to get the current week of the year when all I had to do was int($day_of_year / 7). Sure DateTime has something like that, but why go through all the trouble of setting it up, when something else much simpler is already around?

So called templates

I am sorry, but I can't see how any of these so called template modules really create a template that will fill in everything that I need them to fill in. HTML::Template appears to do only very basic stuff, but since I use so many conditionals and list iterations and loops, it won't work. Template Toolkit doesn't even look like perl, so I will stay as far away from that mess as possible. I don't need to get all mixed up right now. Printing the titles of my pages based on a slew of conditions seems beyond their capabilities, so how could I get them to print my site's menu and the right list of style sheets?

/*SQL*/ databases

Almost everyone I talk to here and elsewhere gets on my case about using a text based database system, with the directory structure being a part of it, over a SQL related database. I have tried to explain that I do not have access to either MySQL or SQLite on the server where my main site is, Xecu.net. When they tell me to move to somewhere that has either, I also try to explain that I can't move because I would lose 3 out of 6 of my reciprocated links. (I don't have many people whose interests coincide with mine, so those few who agreed to reciprocate are appreciated.)

I made attempts to check to see whether or not MySQL or SQLite was available to me on Xecu.net by trying to create a database at the command line. The results follow.

MySQL test...

/users/fantasy>mysql db_name ERROR 2002 (HY000): Can't connect to local MySQL server through socket + '/tmp/mysql.sock' (2)

SQLite test...

/users/fantasy>sqlite3 test.db sqlite3: Command not found.

The above tells me that they are not available, so I had to find an alternative, which I did in separated values files. I would have to have a database and the access and editing s‎crip‎ts for that database to test, but since I can't make a /*SQL*/ database, I can't play with the concept either.

The last problem with /*SQL*/ databases is that they are binary, meaning I would not be able to edit them in my text editor. If I can't get a decent front end up for my text based databases, I would be horribly disadvantaged with a binary one. I would have to keep all the text around somewhere to edit in my text editor just to stuff that text into the binary database. So, I will use what I have available to make databases.

I would like this to be the last time I have to defend my use of separated values. I try to stay away from the database topic as much as possible these days whenever it comes up.

Version control

There are two schools of thought for this one. A few seem to feel that I will crash and burn without it; while others say that since I am working alone, I don't need it. I have looked around and haven't found anything to my liking. There is the whole concept of storing all of the changes to my files that really is not appealing to me. There is also the lack of automation, where the changes are stored whenever I save in my favorite text editor. I spoke to a few of its developers who seemed interested but nothing came of it. Maybe once its integrated into my text editor and automatic, I will give version control a whirl.

Take a break

Wow! You read it all! The rest is just tools I use to write perl. Have a plate of cookies!

Toolbox

Coding Toolbox

  • What I have:
    • Unlimited web space
    • Markup
      • HTML
      • CSS
      • SSI
    • S‎crip‎ting
      • perl
        • 5.20.2 (local)
        • 5.8.8 (web host)
      • javas‎crip‎t
    • Programs
      • geany
      • Filezilla
      • Firefox
  • What I don't have or understand:
    • PHP
    • ASP
    • MySQL
    • SQLite

Quote

"A man's knowledge is like an expanding sphere, the surface corresponding the boundary between the known and the unknown. As the sphere grows, so does its surface; the more a man learns, the more he realizes how much he does not know. Hence, the most ignorant man thinks he knows it all." - L. Sprague de Camp

Geek Code

GO d- s+:+ a C++ UL P+ L W++ N K- w M-- PS+ PE Y+ t+ 5++ X+ R+++ tv+ b +++ D- G e h- r++ x++

Last edited and updated: 2015-05-12


Posts by Lady_Aleena
Chatbox too small in Perl Monks Discussion
1 direct reply — Read more / Contribute
by Lady_Aleena
on Apr 16, 2015 at 17:45

    Today I was trying to lengthen the chatbox on the FullPage Chat. I added the following to my css in my display settings, but the talk button didn't move so the chatbox was under the talk button.

    .chatbox { width: 125%; }

    Can we please get a longer chatbox? Currently only about 40 characters can be seen at a time. A longer chatbox would mean fewer typos, IMO. So pretty please with sugar cookies on top? ")

    No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
    Lady Aleena
Number functions I have lying around in Cool Uses for Perl
2 direct replies — Read more / Contribute
by Lady_Aleena
on Mar 30, 2015 at 20:30

    I was going through old scripts I had lying around and decided to clean them up a bit. I don't remember why I wrote them or what I am going to do with them. I think they are lukewarm uses for perl, and I probably reinvented the wheel on some of them. Instead of them just lying around my hard drive collecting dust, I share them with you, kind reader. Do with them what you will.

    The first function lists primes, the second two functions list fractions, the last few are about Roman numerals. I lumped them together in a module called Numbers because I ran out of imagination.

    Welcome to my sandbox.

    If you want to see a Roman numeral (MDCCCMVCXXVII) with the overline, go to your display settings and set up the overline class in your style sheet as follows...

    .overline { text-decoration: overline; }
    No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
    Lady Aleena
Please let the last message a user sent in the chatterbox time out? in Perl Monks Discussion
2 direct replies — Read more / Contribute
by Lady_Aleena
on Dec 08, 2014 at 02:20

    Hello. Today I went into the chatterbox to say "Hello". My simple "Hello" did not appear. I went to CBstream and found the last message I sent before this "Hello" was another "Hello". I think I sent it a few days ago. While I can see the wisdom behind not allowing repeated messages in the CB, would you please consider putting a time out of something like half a day or a day? Sometimes I don't want to freshen the cookies on the sideboard (and even if I did, I can't do it twice in a row).

    Thank you.

    No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
    Lady Aleena
The hills are alive... in Cool Uses for Perl
4 direct replies — Read more / Contribute
by Lady_Aleena
on Nov 26, 2014 at 22:25

    ..with The Sound of Music. ;)

    I had @SoM_notes and $SoM sitting around doing nothing, so this evening, I made them do something. In make_SoM_song and get_SoM_def, you enter a string of alphabetical notes (c, d, e, f, g, a, b). The notes can be separated by a comma, semicolon, or a space. The functions will return the note name given by Maria von Trapp in The Sound of Music.

    I wrote random_SoM_note and random_SoM_song because I couldnt' help myself. Most of you know how much I love to randomly generate things. :)

    make_SoM_song, get_SoM_def, and random_SoM_song all return array references.

    Enjoy the code!

    package SoundofMusicSong; use strict; use warnings; use Exporter qw(import); our @EXPORT_OK = qw(make_SoM_song get_SoM_def random_SoM_note random_S +oM_song); my @base_notes = qw(c d e f g a b); my @SoM_notes = qw(do re me fa so la te); my %notes; @notes{@base_notes} = @SoM_notes; my $SoM = { 'do' => 'a deer a female deer', 're' => 'a drop of golden sun', 'me' => 'a name I call myself', 'fa' => 'a long long way to run', 'so' => 'a needle pulling thread', 'la' => 'a note to follow so', 'te' => 'a drink with jam and bread', }; sub make_SoM_song { my ($user_song) = @_; my @song_notes = split(/[ ,;]/, $user_song); my @new_song = map { $_ = $_ =~ /^[a-g]$/ ? $notes{$_} : 'not a note +'; $_ } @song_notes; return \@new_song; } sub get_SoM_def { my ($user_song) = @_; my $notes = make_SoM_song($user_song); my @new_song = map { $_ = $$SoM{$_} ? $_.' '.$$SoM{$_} : 'not a note +'; $_ } @$notes; return \@new_song; } sub random_SoM_note { my $note = $SoM_notes[rand @SoM_notes]; return $note; } sub random_SoM_song { my ($number_of_notes) = @_; my $notes = $number_of_notes ? $number_of_notes : int(rand(100)) + 1 +; my @new_song; push @new_song, random_SoM_note for (1..$notes); return \@new_song; } 1;
    No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
    Lady Aleena
I have a list expanding when I don't want it to in Seekers of Perl Wisdom
1 direct reply — Read more / Contribute
by Lady_Aleena
on Nov 19, 2014 at 16:26

    Hello everyone.

    In the following function, I create two new keys for a hash which is entered into the function.

    use List::Util qw(shuffle); sub random { my ($user_input, $list) = @_; $list->{'all'} = [ map { @$_ } values %{$list} ]; $list->{'keys'} = [ grep {$_ !~ /(?:all|keys)/} keys %{$list} ]; if ($user_input && $user_input =~ /(?:help|options)/) { return 'Your options are: '.join(', ',sort @{$$list{'keys'}}).', o +r all.'; } elsif ($user_input && $user_input =~ /dump/) { use Data::Dumper; return Dumper($list); } else { my $input = $user_input ? $user_input : 'all'; my @random_list = shuffle(@{$$list{$input}}); return $random_list[rand @random_list]; } }

    The first time I run a list through random, the list for $list->{'all'} is just what I want it to be. However, upon subsequent runs of random, 'all' grows. I can't figure out why it is doing it. Here is an example...

    my %waters = ( running => [qw(spring streamlet rivulet run brook creek stream rive +r)], standing => [qw(drop puddle pool pond lake sea ocean)], precipitation => [qw(rain snow sleet hail)] ); sub random_water { my ($user_water) = @_; random($user_water, \%waters); }

    After I run random_waters('dump'), I get...

    The hash looks good here, however, when I run it twice...

    'all' has grown, it even includes the keys, which shouldn't be possible as I create 'keys' after I create 'all'. I can't figure out why. %waters should grow between runs, right?

    No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
    Lady Aleena
RFC: Is there a better way to use Text::Balanced? in Seekers of Perl Wisdom
3 direct replies — Read more / Contribute
by Lady_Aleena
on Nov 12, 2014 at 22:49

    Hello. I have been writing a function to help me parse my lines. I had gotten help on this previously, however, I could not get it to work in Perl 5.8.8. I gave Text::Balanced a try and got it to work on my system. (I haven't uploaded the new code to my web host yet. I am trying to manage my expectations.) Before I get too excited about it, I ask for someone to look it over and let me know where I may encounter problems, if any. Have a cookie.

    Here is the string I ran through my new inline function for the examples. I know it is long, but I will be parsing longer strings through inline.

    The first thing I wanted to do was make sure any inline HTML comments I put in just for me are stripped. So I remove those first with s/\s\<!.+$// on line 15. Now the party can really start by extracting the brackets on line 17.

    Since my lines can have more than one set of brackets in them, I have to use extract_multiple in conjunction with extract_bracketed. So, using those together, I get the following array.

    I was looking at it, and in moments I figured out how to group the results. It appears every other value is what is the bracket found with the value before it being what comes before the bracket. The last value is what appears after the last bracket, if anything. To make it a little easier for me to munge the lines, I need to group them. So I wrote a little function called fancy_splice to group the lines together.

    So, after putting the array returned by extract_multiple through fancy_splice, I get...

    Now the party is in full swing with my lines with partners with the exception of the last guy standing alone at the end in this case. Because he is such a party pooper, I am going to deal with him first in lines 17 to 23 by assigning his value to $end. I pop off his group from the rest and send him to the bar.

    Now I turn on the music and get the pairs dancing on line 25. The first partner I deal with is the lead, $bracket->[0]. On his tails is the tag which goes with the contents of the bracket (the last word of the value). So, I assign everything else in the lead to $start and put the last word into $tag.

    Now, we will have to skip two loops ahead here to explain why $tag also needs munging. If you look at the first group's second value, you will see EurSUP. Well EurSUP is not an inline HTML tag, and I just want the SUP. So, lines 30 through 36 deal with getting the tags I want and rejoining the part, which is not the tag, with $start.

    Now I am done dealing with the lead, it is time to deal with his partner which takes a bit more work. The first things I need to do are to name her $tagged and take her brackets off (line 38). I next look to see if the tag has any attributes, so I split $tagged by the pipes, in any. I then check the last value of the array has a > in it, and if not, pop it off the array and assign it to the $attribute. (If it did have a > in it, then it needs to be kept with the $in_tag because it may be the attribute of the next level tag or even the level after next.

    When the dance is done for on set, I put the pieces together on line 43 and concatenate that piece to $line. When all the sets are done dancing, I bring $end back from the bar, and concatenate him too. All the dancing produces...

    Thank you for taking the time to read this, have another cookie. Please let me know if you see a problem or a place where I can tighten things up.

    No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
    Lady Aleena
Using Data::Compare recursively to better identity differences between hashes in Cool Uses for Perl
2 direct replies — Read more / Contribute
by Lady_Aleena
on Oct 18, 2014 at 01:49

    Yesterday I wanted to compare two hashes to see if they were the same. I looked around a little bit and found Data::Compare. It was good at telling me the two hashes were different, however, it did not tell me where. So, I wrote a small little subroutine to recursively check my hash of hashes (of hashes). It was able to identity where I had to look to make corrections, almost to the exact spot. (I am unsure how to compare arrays of hashes just yet which is why the following little subroutine will almost take you to the right spot.)

    There are still holes in the script, but it worked for me today.

    #!/usr/bin/perl use strict; use warnings FATAL => qw( all ); use Data::Compare; use Data::Dumper; # You can take out all instances of the subroutine 'line' to print wha +t you want in those places. sub deep_data_compare { my ($tab, $old_data, $new_data, $data) = @_; my $old = $old_data; my $new = $new_data; my $compare = new Data::Compare($old, $new); if ($compare->Cmp == 0) { line($tab,$data) if $data; if (ref($old) eq 'HASH' && ref($new) eq 'HASH') { line($tab+1,'old to new'); for (keys %$old) { deep_data_compare($tab+2,$_,$$old{$_},$$new{$_}); } } # I have not figured out this part yet. # elsif (ref($old) eq 'ARRAY' && ref($new) eq 'ARRAY') { # } else { print Dumper($new); print Dumper($old); } } } sub rline { my ($tab,$line) = @_; return qq(\t) x $tab.qq($line\n); } sub line { print rline(@_); } deep_data_compare(0, \%old_hash, \%new_hash, 'widgets');
    No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
    Lady Aleena
Add social networks to home nodes in Perl Monks Discussion
2 direct replies — Read more / Contribute
by Lady_Aleena
on Jan 20, 2014 at 14:10

    Recently I was asked by another monk for a Twitter to PerlMonks user handle decoder. I would love to be able to store my social network user names by my avatar instead of mixing it in with the text of my profile. Also, if a Monk fills in those fields, it might also be nice for a meta page with a list like Monks with Twitter accounts.

    TwitterLady_Aleena
    Google+LadyAleena Noyb
    TumblrLadyAleena
    GetGlueLady_Aleena
    FoursquareLady_Aleena

    Facebook, Linkdin, etc. can be optional fields too with the accompanying "Monks with X accounts" pages. Just a thought.

    No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
    Lady Aleena
Need help with filling a complicated data structure in Seekers of Perl Wisdom
5 direct replies — Read more / Contribute
by Lady_Aleena
on Nov 16, 2013 at 01:06

    Hello everyone. Before you start reading, have a Cookie and take a deep breath. Ready?

    I am trying to make my data more universal by removing as many HTML tags out of my data and munging it later. For the most part I have succeeded. However, there are a few more hurdles I have yet to clear, and the following is one of them.

    I would like to take this following data ...

    # * is an unordered list # # is an ordered list # #3 a number indicates the value __DATA__ * list 1 unordered item 1 * list 1 unordered item 2 *# list 1 unordered item 2 ordered item 1 *# list 1 unordered item 2 ordered item 2 *# list 1 unordered item 2 ordered item 3 * list 1 unordered item 3 ** list 1 unordered item unordered item 1 ** list 1 unordered item unordered item 2 ** list 1 unordered item unordered item 3 **# list 1 unordered item unordered item 3 ordered item 1 **# list 1 unordered item unordered item 3 ordered item 2 **# list 1 unordered item unordered item 3 ordered item 3 # list 2 ordered item 1 #3 list 2 ordered item 2 # list 2 ordered item 3 #* list 2 ordered item 3 unordered item 1 #* list 2 ordered item 3 unordered item 2 #* list 2 ordered item 3 unordered item 3

    ... and feed it into this subroutine, which I got help for here, hopefully between lines 6 and 17 ...

    sub story { my ($source, $doc_magic, $line_magic) = @_; my $inc = 0; my @sections; my @toc; while (my $line = <$source>) { chomp($line); next if !$line; if ($line =~ /^2/) { my ($number,$text) = split(/ /,$line,2); push @toc, anchor(textify($text), { href => '#'.idify($text) }); $inc++; } push @{$sections[$inc]}, $line; } my $tab = 3; $inc = 0; for my $section (@sections) { if ($section) { section($tab, sub { $tab++; for my $line (@{$section}) { my $line = convert_string($line, $line_magic); line($tab, $line), next if $line =~ /^</; line($tab, "<$line>"), next if $line =~ /^[bh]r$/; $doc_magic->{$1}->(), next if $line =~ /^&\s+(.*)/; blockquote($tab, $1), next if $line =~ /^bq\s(.*)/; item($tab + 1, $1), next if $line =~ /^\*\s(.*)/; item($tab + 1, $2, { value => $1 }), next if $line =~ /^\*(\ +d+)\s(.*)/; item($tab + 1, "<strong>$1</strong> $2"), next if $line =~ / +^\*s\s(.+\:)\s(.*)$/; heading($tab, $1, $2, { id => idify($2) }), next if $line + =~ /^([1-6])\s+(.*)/; paragraph($tab, $line, { class => 'author' }), next if $line + =~ /^by /; paragraph($tab, $line); } $tab--; }); } if ($inc == 0 && @toc > 3) { section($tab, sub { my $class = @toc > 25 ? @toc > 50 ? 'four' : 'three' : 'two'; my $style = @toc > 50 ? 'font-size:smaller' : undef; list($tab + 1, 'u', \@toc, { class => $class, style => $style +}); }, { class => 'contents'} ); } $inc++; } # paragraph($tab,"written by $root_user", { class => 'author' }); }

    ... which will hopefully feed the following data structures through lines 22 through 42 above. Take another deep breath, by the way ...

    my $list1 = [ 'u', [ 'list 1 unordered item 1', [ 'list 1 unordered item 2', { 'inlist' => [ 'o', [ 'list 1 unordered item 2 ordered item 1', 'list 1 unordered item 2 ordered item 2', 'list 1 unordered item 2 ordered item 3' ] ] } ], [ 'list 1 unordered item 3', { 'inlist' => [ 'u', [ 'list 1 unordered item unordered item 1', 'list 1 unordered item unordered item 2', [ 'list 1 unordered item unordered item 3', { 'inlist' => [ 'o', [ 'list 1 unordered item unordered item 3 ordered item 1', 'list 1 unordered item unordered item 3 ordered item 2', 'list 1 unordered item unordered item 3 ordered item 3' ] ] }, ] ] ] }, ] ] ]; my $list2 = [ 'o', [ 'list 2 ordered item 1', ['list 2 ordered item 2', { value => '3' } ], [ 'list 2 ordered item 3', { inlist => [ 'u', [ 'list 2 ordered item 3 unordered item 1', 'list 2 ordered item 3 unordered item 2', 'list 2 ordered item 3 unordered item 3', ] ] } ] ] ];

    ... into ...

    sub list { my ($tab,$type,$list,$opt) = @_; my $tag = $type.'l'; my $open = open_tag($tag,$opt,[@ics,@java]); line($tab,"<$open>"); for my $item (@$list) { if (ref($item) eq 'ARRAY') { item($tab + 1,$item->[0],$item->[1]); } else { item($tab + 1,$item); } } line($tab,"</$tag>"); }

    which is dependent on...

    sub item { my ($tab,$value,$opt) = @_; my $tag = 'li'; my $open = open_tag($tag, $opt, ['value', @ics, @java]); line($tab, "<$open>"); line($tab + 1, $value); if ($opt->{inlist}) { list($tab + 1, @{$opt->{inlist}}); } line($tab,"</$tag>"); }

    ... with instructions on how to use them somewhere in here.

    I am tired, cranky, and moody. I can't figure out how to munge the lines. A nudge, a whisper, a gentle turning of the head is all I can ask for here. Please just don't ask me to rewrite my list or item subroutines. I use them elsewhere too.

    After lists are handled, I still have the table and some inline HTML tags to remove by some sort of munging. Those are both a lot more complicated.

    Thanks in advance!

    No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
    Lady Aleena
Group __DATA__ lines in a while? in Seekers of Perl Wisdom
1 direct reply — Read more / Contribute
by Lady_Aleena
on Oct 21, 2013 at 01:22

    Update: I'm putting this update at the top because this is a longish post. I think I got it, and I put how I got it in a reply below. Sometimes I just have to type what I want out to get some perspective.

    Hello. I am looking for pointers for the best way to group __DATA__ lines while looping them through a while to create sections. In the sample data below, the first three lines are the lede, which I would like to be the first section. The fourth line starts with a 2 which indicates the first section header. I would like to group the header and the lines following until the next line which starts with a 2 in a section. I would run each section through the while in story. Not all stories have headings, so a whole story might be one section; not all stories have ledes, so the first section may have a heading. I don't know if this is a good idea, but maybe have a way to do a quick pass through the data looking for lines which start with 2, if there aren't any, just run it through without separating it into sections.

    While I am at it, I would also like to grab all the lines which start with a 2 to print a table of contents if there are 3 or more headings. Maybe the two things can be done at the same time?

    story is the most used subroutine after html on my site, so I'm starting with it first. I'm updating my styles, which means I need to divide my stories up.

    Here is what I have.

    sub story { my ($source,$author) = @_; my $tab = 3; while (my $line = <$source>) { chomp($line); # If a line starts with a bracket, just print the line. if ($line =~ m/^</) { line($tab,$line); } # If a line starts with a numeral 1-6, print a heading. elsif ($line =~ /^[1-6]\s/) { my ($heading,$text) = split(/ /,$line,2); my $id = idify($text); heading($tab,$heading,$text, { id => $id }); } # If a line is a break or horizontal rule, print the line in brack +ets. elsif ($line =~ /^[bh]r$/) { line($tab,"<$line>"); } # All other lines are paragraphs. else { paragraph($tab,$line); } } # If I wrote a story, I want people to know I wrote it at the bottom +. paragraph($tab,"written by $root_user", { class => 'author' }) if !$ +author; }

    Here how to use the above (with a few other things).

    #!/usr/bin/perl use strict; use warnings FATAL => qw( all ); use CGI::Carp qw(fatalsToBrowser); use lib 'files/lib'; use Base::HTML qw(html story); html( code => sub { story(*DATA,'me') });

    And here is sample data.

    No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
    Lady Aleena
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2017-05-26 06:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?