CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Foreach loop is not nested properly

 



balrajswamy
Novice

Dec 16, 2015, 9:45 AM

Post #1 of 3 (1385 views)
Foreach loop is not nested properly Can't Post

Hi All,
Good morning.
I am struggling since 3 days to get expected output for the below codes. if some one go through the codes and help me to get expected output if possible.

It will be helpful

1. perl code:

Code
#!c:/perl/bin/perl -w 
use warnings;
#use strict;
use CGI ':standard';
use List::MoreUtils qw(uniq);

print "Content-type: text/html\n\n";
#system("cls");

@aa= qw(
2009R1073_002.004.0_EN_LayoutOL
2009R1073_002.004.0_BG_LayoutOL
2009R1074_002.004.0_NL_MLConSolid
2009R1073_002.004.0_NL_LayoutOL
2009R1074_002.004.0_BG_MLConSolid
2009R1073_002.004.0_EL_MLXMLQC
2009R1074_002.004.0_NL_LayoutOLML
2009R1073_002.004.0_ES_XMLDispatch
2009R1073_002.004.0_PT_XMLDispatch
2009R1073_002.004.0_ET_MLXMLQC
);

@families=();
foreach $aa (@aa)
{

$temp = substr($aa, 0,19);
push (@families, $temp);
}

@uni_items= uniq @families;

@bb= qw(
2009R1073_002.004.0_EN_LayoutOL
2009R1073_002.004.0_BG_LayoutOL
2009R1074_002.004.0_NL_MLConSolid
2009R1073_002.004.0_NL_LayoutOL
2009R1074_002.004.0_BG_MLConSolid
2009R1073_002.004.0_EL_MLXMLQC
2009R1074_002.004.0_NL_LayoutOLML
2009R1073_002.004.0_ES_XMLDispatch
2009R1073_002.004.0_PT_XMLDispatch
2009R1073_002.004.0_ET_MLXMLQC);

@dd=();
print "<table border=1 align= center width=50% bgcolor=beige><tr><td colspan=3 align=center><h3>Table data</h3></td></tr><tr><td>Status</td><td>No_LVs</td><td>LVS's Name</td></tr>";

foreach $uni (@uni_items)

{

print "$i $uni<br>";

foreach $bb (@bb)

{

if ($bb=~m/$uni(.*)/)

{

push (@dd, $bb);

#print "$bb<br>";

}

}
}

print "<br> +++++++++++++++++++++<br>";

#print "\n temp $temp\n";
foreach $line (@dd)
{
@array=split("_", $line);
$char = $array[2];
$num=$array[3];

if($hash{$num}){
$hash{$num}[0]++;
$hash{$num}[1]=$hash{$num}[1].",".$char;

}
else {
$hash{$num}=[1, $char];
}

}


for $key ( keys %hash ) {

print "<tr><td>$key<\/td>" , join("<\/td> <td>", "</td>" , @{$hash{$key}}), "<\/td><\/tr>";




}




2. expected output shown as output.png

3. generated output is shown as test6.png

Thanks in advance.


(This post was edited by FishMonger on Dec 16, 2015, 2:35 PM)
Attachments: output.PNG (13.6 KB)
  test6.PNG (17.4 KB)


FishMonger
Veteran / Moderator

Dec 16, 2015, 2:39 PM

Post #2 of 3 (1378 views)
Re: [balrajswamy] Foreach loop is not nested properly [In reply to] Can't Post

I can't look into this right now, but the first thing you need to do is to uncomment the use strict statement and fix all of the problems it points out, which for the most part is needing to declare the vars with the my keyword.

You also need to fix the indentation.


Zhris
Enthusiast

Dec 16, 2015, 4:24 PM

Post #3 of 3 (1368 views)
Re: [balrajswamy] Foreach loop is not nested properly [In reply to] Can't Post

Thats some pretty convoluted code you've got there!

- I think you need to increase your grasp on hashes and their many uses. Hashes can represent multi-dimensional data, but most importantly here, their keys must be unique and this feature can be used to handle duplicates appropriately.

- My preference is nearly always to build up a single data structure then deal with printing it, separating the two core pieces of logic. A template module is the ideal solution here, though it does require learning a new syntax. In my attempt below I handle certain logic in template i.e. counting the number of elements and joining the language locales, I would usually push this data into the hash beforehand, especially if I might need to switch the template with another serialization method.

- Reading through your code I see no purpose in having two duplicate arrays, @aa and @bb, it doesn't look as though you modified @aa at any point. This added to my confusion.

Here is my attempt, the core section you need to look at is the for loop, its up to you how you want to handle output.


Code
#!/usr/bin/perl 
use strict;
use warnings;
use Template;

my $list =
[ qw/
2009R1073_002.004.0_EN_LayoutOL
2009R1073_002.004.0_BG_LayoutOL
2009R1074_002.004.0_NL_MLConSolid
2009R1073_002.004.0_NL_LayoutOL
2009R1074_002.004.0_BG_MLConSolid
2009R1073_002.004.0_EL_MLXMLQC
2009R1074_002.004.0_NL_LayoutOLML
2009R1073_002.004.0_ES_XMLDispatch
2009R1073_002.004.0_PT_XMLDispatch
2009R1073_002.004.0_ET_MLXMLQC
/ ];

my $hash = { };

# iterate each row in list.
for my $row ( @$list )
{
# split the row on underscores that are not followed by a digit.
my ( $id, $lang, $status ) = split /_(?!\d)/, $row;

# construct a three level hash that represents the datas hierarchy.
$hash->{$id}->{$status}->{$lang} = 1;
}

# instantiate template module.
my $template = Template->new or die $Template::ERROR;

# process and output template.
print "content-type: text/html\n\n";
$template->process( \*DATA, { hash => $hash } ) or die $template->error;

__DATA__
<html>
<head>
<title>Table data</title>
<style type="text/css">
table, th, td
{
font-family: Arial;
font-size: 10pt;
border-collapse: collapse;
border: 1px solid black;
}
</style>
</head>
<body>
<table>
<tr>
<td colspan="3">Table Data</td>
</tr>
<tr>
<th>Status</th>
<th>NO_LVs</th>
<th>LVS's Name</th>
</tr>
[%- FOREACH id IN hash.keys.sort %]
<tr>
<td colspan="3">[% id %]</td>
</tr>
[%- FOREACH status IN hash.$id.keys.sort %]
<tr>
<td>[% status %]</td>
<td>[% hash.$id.$status.size %]</td>
<td>[% hash.$id.$status.keys.sort.join( ', ' ) %]</td>
</tr>
[%- END; END %]
</table>
</body>
</html>


Chris


(This post was edited by Zhris on Dec 16, 2015, 4:30 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives