Breaking a list down

They have: 21 posts

Joined: Nov 2004

This is beyond me.

I have a list like this:
200611
200612
200701
200702
Etc.

I need to pull out the year (i.e., 2006) and only print it only once. The other 2 digits represent the months and all these need to be shown. So I can have something along these lines:

2006
11
12
2007
01
02

Is there a simple way to do that in Perl please?

thanks - Ted

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

Here's a quick attempt at it. The function [incode]group_lines_by_year[/incode] takes an array of lines as you showed and returns an array of reorganised lines that you can print any way you like.

sub group_lines_by_year {
    @ret = (); $current_group = null;
    foreach (@_) {
        /([0-9]{4})([0-9]{2})/;
        ($year, $month) = ($1, $2);
        if ($year != $current_group) {
            $current_group = $year;
            push (@ret, $year);
        }
        push (@ret, $month);
    }
    return @ret;
}

@lines = ('200611','200612','200701','200702');
print join("\n", group_lines_by_year(@lines)) . "\n";
'

It works simply by walking through every line and checking if the date is in the "current group". If it isn't, then it updates the current group. Each year and month is collected in an array and returned.

Smiling

They have: 21 posts

Joined: Nov 2004

Thanks Abhishek, that works great. I did a text file with several yearsmonths, read into @lines array and got a nice list.

Is there a way I can add links to each element? Your code is bit tight. I did manage line breaks like so:

print join("<br>\n", group_lines_by_year(@lines)) . "\n";
'

But couldn't get any further.

The end result will reside in a list that a user can click the year or the month and an action will take place. The year link will cause the months to drop down (JS and/or CSS) and the month links will open a file.

I'm more a designer than programmer, but manage to get some Perl working.

Thanks again - Ted

He has: 1,380 posts

Joined: Feb 2002

(not related, but you should try PHP Wink things would be much simpler if you didn't have to code in Perl...just a thought)

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

You don't have to print it as I gave in my example. You could do something like this:

foreach (group_lines_by_year(@lines)) {
    print "<a href=\"foo\">" . $_ . "</a><br />\n";
}
'

The point is, the function [incode]group_lines_by_year[/incode] returns a list, with which you can do anything you want. Smiling

brady.k;218271 wrote: (not related, but you should try PHP Wink things would be much simpler if you didn't have to code in Perl...just a thought)

I disagree. The algorithm would be identical. Its expression in PHP -- aside from Perl's shorthand regex and special variables -- would also be nearly identifcal. The difference is negligible. Smiling

timjpriebe's picture

He has: 2,667 posts

Joined: Dec 2004

As someone who programs in both Perl and PHP on a regular basis, I would have to disagree with Abhishek and agree with brady.k. I've found that for non-programmers or new programmers, PHP is much easier and simpler to pick up.

And if I had my choice, I would be programming exclusively in PHP with no Perl at all. Smiling

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

timjpriebe;218299 wrote: As someone who programs in both Perl and PHP on a regular basis, I would have to disagree with Abhishek and agree with brady.k. I've found that for non-programmers or new programmers, PHP is much easier and simpler to pick up.

The only reason I can think of for why this can be is that documentation and support for PHP is easier to find -- including tutorials, articles and forums. However, owning the right book or searching and asking in the right places is enough to overcome that handicap for Perl.

There is very little intrinsic in either of the languages and systems that makes one easier than the other, for a newcomer. Equally, there are parts that may be harder in one or the other. I'm not even sure libraries (standard or otherwise) really count for or against either choice, as they're roughly of the same breadth and quality.

For the record, here's the function in question written in PHP:

<?php
function group_lines_by_year ($lines) {
 
$ret = array(); $current_group = NULL;
  foreach (
$lines as $line) {
   
preg_match(\"/([0-9]{4})([0-9]{2})/\", $line, $matches);
   
$year = $matches[1]; $month = $matches[2];
    if (
$year != $current_group) {
     
$current_group = $year;
      array_push(
$ret, $year);
    }
    array_push(
$ret, $month);
  }
  return
$ret;
}
?>

It is, for all intents and purposes, exactly the same as the Perl code. I might contend that the Perl version is less verbose and more readable too, but that's a matter of taste and style.

So it's not clear that PHP is in any way advantageous here. There's no compelling reason to make such an arbitrary switch.

Smiling

They have: 21 posts

Joined: Nov 2004

Ok,
This is where I've got to at the moment:

@lines = ('200611','200612','200701','200702');
sub parse_date_line {
/([0-9]{4})([0-9]{2})/;
return ($1, $2);
}

sub group_lines_by_year {
@ret = (); $current_group = null;
foreach (@_) {
($year, $month) = parse_date_line $_;
if ($year != $current_group) {
$current_group = $year;
push (@ret, "<a href\=\"\#nogo\">$year<\/a>");
}
push (@ret, "<a href\=\"\#thearchivecodegoeshere\">$month<\/a>");
}
return @ret;
}
foreach (group_lines_by_year(@lines)) {
print $_ . "<br>\n";
}
'

I still have to see if I can integrate this with the rest of my code. Eventually this will be part of a small blog system (DaBloggo) which people will be able to download free from my site. Abhishek, you'll get listed in the credits for this (unless you don't want to be).

It's all in Beta Version at the moment - so's I can get input from others and stuff right.

cheers - Ted

He has: 1,380 posts

Joined: Feb 2002

Sorry, I didn't mean to get everyone off topic. I just wanted to poke at Perl a little. I mentioned it because I think PHP is easier to read than Perl, and if that's the only real benefit, it's all the more reason to use it. Wink

They have: 21 posts

Joined: Nov 2004

I should have thought of that Abhishek. Thanks. I'll have to try and split that down so the year can have it's blank link and the months their code.

As for the PHP. Learning a bit of Perl does my head in at times, to begin a whole new language . . . . aaargh!

- Ted

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.