Solved

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

Posted on 2003-11-20
4
222 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

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 help 12 104
Perl for loop for 2000 ms 7 100
Search in text file in column and compare 4 47
unable to remove invisible characters with perl 2 84
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 …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

770 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