[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

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 ??

0
Perllearner
Asked:
Perllearner
  • 2
  • 2
1 Solution
 
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
 
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now