Adding <br> when value of field in array changes.

I have data in an array of four fields split on "|"
ex:  ($num, $img, $comment, $date) = split(/\|/, @array)
so, foreach $line (@array)  #  each has \n on the line end
chomp;

Now, when I print the data, I would like to have it insert a
<br> when the value of field 1 ($img) changes :
(these are sorted by field 1 before printing)
ex data:

10|another.gif|nice job|Mon Nov 22 \n
09|another.gif|nice job|Mon Nov 22 \n
03|stillanother..gif|nice job|Mon Nov 22 \n
08|this.gif|nice job|Mon Nov 22 \n
06|this.gif|nice job|Mon Nov 22 \n
so I would like it to print the entire lines as so:

10|another.gif|nice job|Mon Nov 22 \n
09|another.gif|nice job|Mon Nov 22 \n
<br>
03|stillanother..gif|nice job|Mon Nov 22 \n
<br>
08|this.gif|nice job|Mon Nov 22 \n
06|this.gif|nice job|Mon Nov 22 \n
<BR>



The hash Ii use is a simple one for sorting only,
it sorts asciibetical on field 1 and then numeric on
field 0, and since there are several duplicate 'keys'
for the field 1, I cant figure out how to modify it
to fit.  I was thinking of saying
if ($array[3] ne $array[2] or when $array[3] ==9
$line = "<br>$line\n";  etc, but I won't know in
advance how many of each line there will be, and
there may be duplicates of each line.

Can anyone help this newkid out ??

PerllearnerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bcladdCommented:
All you need to do is remember the previous value of filed 1, right? And if it changes, print <br>:

my $previousImg = "";

Then, inside the loop after you pull out $img:

print "<br>\n" if ($img ne $previousImg && $previousImg ne "");
$previousImg = $img;

Hope this helps, -bcl

0
PerllearnerAuthor Commented:
Sorry, but I couldnt get that to work. Could you post an example of how to use it?
0
bcladdCommented:
#!c:/perl/bin/perl
use strict;

my (@array);
push @array, "10|another.gif|nice job|Mon Nov 22 \n";
push @array, "09|another.gif|nice job|Mon Nov 22 \n";
push @array, "03|stillanother..gif|nice job|Mon Nov 22 \n";
push @array, "08|this.gif|nice job|Mon Nov 22 \n";
push @array, "06|this.gif|nice job|Mon Nov 22 \n";

my $line;
my $previousImg = "";

foreach $line (@array) {
    my($num, $img, $comment, $date) = split(/\|/, $line);
    print "<br>\n" if ($img ne $previousImg && $previousImg ne "");
    $previousImg = $img;

    print("$num ... $img ... $comment ... $date");
}

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PerllearnerAuthor Commented:
Thanks that works fine.  It's funny how you can go
a few hundred lines with no problem, and then get
stumped by something like I did.  
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.