Solved

how to print out list of lists

Posted on 1998-08-20
3
144 Views
Last Modified: 2010-03-05
.......
push(@$list{$_},$name);
......
how can I print out all elements of this list of lists? Thanks.
0
Comment
Question by:tian
3 Comments
 

Expert Comment

by:kaijen
ID: 1204422
I'm not sure what you're doin' with the push statement. I think $name should be something like ($name) or another slice.
Am I right?

In general printing LoLs works like:

@strArray = ("a;b;c;d", "e;f;g;h", "i;j;k" );

for $i ( 0 .. 2 )
{
  push @LoL, [ split /;/, $strArray[$i] ];
}

for ( @LoL )
{
  for ( @$_ )
    {
      print "$_\n";
    }
}

Best regards,
Kai.
0
 

Author Comment

by:tian
ID: 1204423
Thanks Kai,
I still can not solve it seeing your comment.(maybe i am too stupid)
In fact What I am trying to do is:(you can see my last question:"implement it in elegant way"for reference)

    ....
 foreach $f (@files){
    $name=$f;
    $open(FILE,$f) || die "cannot open";

    while(<FILE>){
      if(exists $pattern{$_}){
         push @{$list{$_}},$name; # push(@$list{$_},$name); doesnot work
      }
    }
}

   ....
 #then how to print content of the @list{pattern1} @list{pattern2}.......
 
0
 
LVL 5

Accepted Solution

by:
b2pi earned 50 total points
ID: 1204424
use strict;


my($i, $j, %list, $ndx);
$i = 0;
foreach $ndx (qw(a b c d)) {
    for ($j = $i; $j < $i +4;$j++) {
      push(@{$list{$ndx}}, $j);
    }
    $i += 4;
}

## Now print it
print "Printing:\n";
foreach $ndx (sort keys %list) {
    print "$ndx:";
    foreach (@{$list{$ndx}}) {
      print "\t$_\n";
    }
    for ($i = 0; $i <= $#{$list{$ndx}}; $i++) {
      print "\t$list{$ndx}->[$i]\n";
    }
    print "\n";
   
}

0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Existing perl code to be changed for ftp to sftp handling 14 214
Strange perl issue 6 130
Merge files & delete row based on criteria using Perl. 1 104
Perl Untar File 1 55
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…

821 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