Solved

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

Posted on 2003-11-20
4
223 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
  • 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 125 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl script to parse log and delete the file 17 176
PERL variable conundrum 9 94
Using Perl DBI to query oracle 3 46
quoting a comma separated list 20 86
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…

809 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