?
Solved

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

Posted on 2003-11-20
4
Medium Priority
?
226 Views
Last Modified: 2010-03-04
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
Comment
Question by:Perllearner
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 11

Expert Comment

by:bcladd
ID: 9788948
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
 

Author Comment

by:Perllearner
ID: 9790715
Sorry, but I couldnt get that to work. Could you post an example of how to use it?
0
 
LVL 11

Accepted Solution

by:
bcladd earned 500 total points
ID: 9790971
#!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
 

Author Comment

by:Perllearner
ID: 9791201
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question