You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by "F.H" <is...@netscape.net> on 2001/06/12 23:19:41 UTC

Is this feasible in Perl??

Hi All,
I have a text file like this:

Name ,123-43-4352, JX, 1234
Sports,123-43-4352, SKI, BaseBall, swimming
Hobbies, 123-43-4352, H1,H2, H3
Hobbies, 123-43-4352, HH, HHH, HHHH2
Hobbies,123-43-4352, H1,H43

Name ,223-63-9352, JX, 1234
Sports,223-63-9352, SKI, BaseBall, swimming
Hobbies, 223-63-9352, H1,H2, H3
Hobbies, 223-63-9352, HH, HHH, HHHH2  
Here is my issue, I am trying to access each record in this file by SSN
ie: (223-63-9352)
I want to be able to say:
for SSN = 223-63-9352,  for Hobbies4 4th entry is HGFR.

while ($line = <INPUT>){


    @line = &parse_line(',',0,$line);
      if ($line[0] =~ /^NAME/gi){
        $ssn = $line[1] ;
          push(@ssns, $ssn);

              }

        if ($line[0] =~ /^Sports/gi ){

          push(@sports, $line)

        }

        if ($line[0] =~ /^Hobbies/gi){

         push(@hobbies, $line)

       }

for ($i = 0; $i<= $#ssns; $i ++)
 {
   print "For  $ssns[$i]\n";

        for ($i = 0; $i <= $#Hobbies ; $i++){ 
       
                   if ($Hobbies[$i] =~ $ssns[$i])
                   { print "Hobbies are: $Hobbies[$i]\n";}
}


I wanted to print out for each SSN corresponding SSN numbers but it's just
not working!!! If this is feasible in Perl I'd appreciate if someone could help.


Thanks                                          
I.S

__________________________________________________________________
Get your own FREE, personal Netscape Webmail account today at http://webmail.netscape.com/

Re: Is this feasible in Perl??

Posted by alastair <al...@calliope.demon.co.uk>.
On Tue, Jun 12, 2001 at 05:19:41PM -0400, F.H wrote:
> 
> Hi All,
> I have a text file like this:

As has been stated, this question is off-topic for a modperl mailing
list. To answer your question - yes, perl was designed to handle this
type of problem. However, you'll need to spend some time and effort to
learn perl before getting things right. Trust me though - it's worth it.
Have a look over here ;

http://learn.perl.org/

and consider joining the 'beginners' list.

Good luck!


-- 
Alastair                            | 
alastair@calliope.demon.co.uk       |
http://www.calliope.demon.co.uk     |            PGP Key : A9DE69F8
-------------------------------------------------------------------

Re: [OT] Is this feasible in Perl??

Posted by will trillich <wi...@serensoft.com>.
On Wed, Jun 13, 2001 at 06:57:45AM +0800, Gunther Birznieks wrote:
> My experience is that architecturally Perl cannot handle this. You should 
> switch to Java and use an Enterprise Java Bean to do all this for you.

you must have an interesting sense of humor.

-- 
I figure: if a man's gonna gamble, may as well do it
without plowing.   -- Bama Dillert, "Some Came Running"

will@serensoft.com
http://sourceforge.net/projects/newbiedoc -- we need your brain!
http://www.dontUthink.com/ -- your brain needs us!

Re: [OT] Is this feasible in Perl??

Posted by Gunther Birznieks <gu...@extropia.com>.
My experience is that architecturally Perl cannot handle this. You should 
switch to Java and use an Enterprise Java Bean to do all this for you.

My hint to you is use a hash where the key is a SSN# to help you. Of 
course, your description of what you want to accomplish is quite ambiguous 
so who knows whether this will really help you.  But I notice you are 
attempting to do a lot of stuff with arrays when I think hash would help.

Anyway, this isn't the place to post non mod_perl questions anyway. Please 
find a normal Perl syntax mailing list please and do not clutter up this 
list with Perl newbie questions -- this is a mod_perl list not a Perl 
syntax list. Perl.com has links to a lot of these resources.

At 05:19 PM 6/12/01 -0400, F.H wrote:

>Hi All,
>I have a text file like this:
>
>Name ,123-43-4352, JX, 1234
>Sports,123-43-4352, SKI, BaseBall, swimming
>Hobbies, 123-43-4352, H1,H2, H3
>Hobbies, 123-43-4352, HH, HHH, HHHH2
>Hobbies,123-43-4352, H1,H43
>
>Name ,223-63-9352, JX, 1234
>Sports,223-63-9352, SKI, BaseBall, swimming
>Hobbies, 223-63-9352, H1,H2, H3
>Hobbies, 223-63-9352, HH, HHH, HHHH2
>Here is my issue, I am trying to access each record in this file by SSN
>ie: (223-63-9352)
>I want to be able to say:
>for SSN = 223-63-9352,  for Hobbies4 4th entry is HGFR.
>
>while ($line = <INPUT>){
>
>
>     @line = &parse_line(',',0,$line);
>       if ($line[0] =~ /^NAME/gi){
>         $ssn = $line[1] ;
>           push(@ssns, $ssn);
>
>               }
>
>         if ($line[0] =~ /^Sports/gi ){
>
>           push(@sports, $line)
>
>         }
>
>         if ($line[0] =~ /^Hobbies/gi){
>
>          push(@hobbies, $line)
>
>        }
>
>for ($i = 0; $i<= $#ssns; $i ++)
>  {
>    print "For  $ssns[$i]\n";
>
>         for ($i = 0; $i <= $#Hobbies ; $i++){
>
>                    if ($Hobbies[$i] =~ $ssns[$i])
>                    { print "Hobbies are: $Hobbies[$i]\n";}
>}
>
>
>I wanted to print out for each SSN corresponding SSN numbers but it's just
>not working!!! If this is feasible in Perl I'd appreciate if someone could 
>help.
>
>
>Thanks
>I.S
>
>__________________________________________________________________
>Get your own FREE, personal Netscape Webmail account today at 
>http://webmail.netscape.com/

__________________________________________________
Gunther Birznieks (gunther.birznieks@eXtropia.com)
eXtropia - The Open Web Technology Company
http://www.eXtropia.com/


Re: Is this feasible in Perl??

Posted by Franck PORCHER <fp...@esoft.pf>.
Perl developpers are helpful. Eventhough this list is not dedicated
to stricto sensu Perl programming, I will answer your question, but just
this time. Please refer to the right list (I thing there is a Perl
beginners list), and above all, get the Camel book (Programming Perl
3rd Edition, O'Reilly editors), and take the time to learn this
superb language. It's more than worth it...

So, your statement :

> Name ,123-43-4352, JX, 1234
> Sports,123-43-4352, SKI, BaseBall, swimming
> Hobbies, 123-43-4352, H1,H2, H3
> Hobbies, 123-43-4352, HH, HHH, HHHH2
> Hobbies,123-43-4352, H1,H43
>
> Name ,223-63-9352, JX, 1234
> Sports,223-63-9352, SKI, BaseBall, swimming
> Hobbies, 223-63-9352, H1,H2, H3
> Hobbies, 223-63-9352, HH, HHH, HHHH2
> Here is my issue, I am trying to access each record in this file by SSN
> ie: (223-63-9352)
> I want to be able to say:
> for SSN = 223-63-9352,  for Hobbies4 4th entry is HGFR.

# A solution
# 1. Build a convenient data structure (we suppose that attribute names are normalized)
our %data = ();
while ( <STDIN> ) {
	my ($attribute, $ssn, @values ) = split /\s*,\s*/;
	push @{ $data{$ssn}{$attribute} }, @values;
}

# 2. Then fetch from it (you could write specialized procs for this, or
# even methods if you consider making a component out of this)
my $hobby_fourth = $data{'223-63-9352'}{'Hobbies'}[3]; # By default, Perl starts array indexes at 0

You see, it's quite short.


Good luck.

Franck

--------------------
Franck PORCHER
Essential Software
http://www.esoft.pf/
Tél: (689) 56 23 95


Re: Is this feasible in Perl??

Posted by Cees Hek <ce...@sitesuite.org>.
On Tue, 12 Jun 2001, F.H wrote:

> 
> Hi All,
> I have a text file like this:
> 
> Name ,123-43-4352, JX, 1234
> Sports,123-43-4352, SKI, BaseBall, swimming
> Hobbies, 123-43-4352, H1,H2, H3
> Hobbies, 123-43-4352, HH, HHH, HHHH2
> Hobbies,123-43-4352, H1,H43
> 
> Name ,223-63-9352, JX, 1234
> Sports,223-63-9352, SKI, BaseBall, swimming
> Hobbies, 223-63-9352, H1,H2, H3
> Hobbies, 223-63-9352, HH, HHH, HHHH2  
> Here is my issue, I am trying to access each record in this file by SSN
> ie: (223-63-9352)
> I want to be able to say:
> for SSN = 223-63-9352,  for Hobbies4 4th entry is HGFR.


You will want to learn how to use hashes instead of just using
arrays.  Give the following (untested code) a try:

# Gather the data
my $data = {};
while ($line = <INPUT>) {
	chomp $line;
	my ($key, $ssn, @values) = split /\s*,\s*/, $line;
	print STDERR "Unparsable record:  $line\n" 
		unless $key =~ /^(name|sports|hobbies)$/i;
	$data->{$ssn}->{lc $key} = @values;
}

# print the data
foreach my $ssn (keys %$data) {
	print "SSN:  $ssn\n";
	print "Name:  ", join ', ', @{$data->{ssn}->{name}}, "\n";
	print "Hobbies:  ", join ', ', @{$data->{ssn}->{hobbies}}, "\n";
	print "Sports:  ", join ', ', @{$data->{ssn}->{sports}}, "\n";
	print "\n";
}


Like I said, this is untested code , but it should give you an idea of
what you can do.

Cees


> 
> while ($line = <INPUT>){
> 
> 
>     @line = &parse_line(',',0,$line);
>       if ($line[0] =~ /^NAME/gi){
>         $ssn = $line[1] ;
>           push(@ssns, $ssn);
> 
>               }
> 
>         if ($line[0] =~ /^Sports/gi ){
> 
>           push(@sports, $line)
> 
>         }
> 
>         if ($line[0] =~ /^Hobbies/gi){
> 
>          push(@hobbies, $line)
> 
>        }
> 
> for ($i = 0; $i<= $#ssns; $i ++)
>  {
>    print "For  $ssns[$i]\n";
> 
>         for ($i = 0; $i <= $#Hobbies ; $i++){ 
>        
>                    if ($Hobbies[$i] =~ $ssns[$i])
>                    { print "Hobbies are: $Hobbies[$i]\n";}
> }
> 
> 
> I wanted to print out for each SSN corresponding SSN numbers but it's just
> not working!!! If this is feasible in Perl I'd appreciate if someone could help.
> 
> 
> Thanks                                          
> I.S
> 
> __________________________________________________________________
> Get your own FREE, personal Netscape Webmail account today at http://webmail.netscape.com/
> 

-- 
Cees Hek
SiteSuite Corporation
cees@sitesuite.net