[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl-WebCGI] 2 Basic loop question(?)



>I'm opening a tag delimited file which contains a data base (ie: 25 rows x
>30 cols). In order to be able to refer to each 'cell' in the data base (ie:
>$c7 = 3rd row, 7th column'), I'm doing:

tag delimited? Oh. You meant tab. Sorry.

There are lots of ways to do this. You can use nested foreach to handle 
each and every item on each and every line, or you can use the array 
context to grab a whole line at a time, as 
you are sort of doing. 

Instead of that huge cloud (flock? herd?) of named scalars, you could use 
named arrays: 
@a = split( /\t/, $a );
In fact, you could do the split while assigning and get rid of more 
scalars ($a, etc.):

@a = split( /\t/, $datos[0] );

@a is an array. $a is a scalar. They look similar, but only to humans. 
Shall we say Perl knows they are not related to each other? Since @a is 
an array, it can receive the list which split() generates. If you would 
rather use a loop, you can, but you wouldn't use split(). You'd use 
index() and substring(). It would be an interesting exercise, if you have 
the time.

On the other hand, I think you can even grab the whole file at once with 
the right split and stuff, but since I'm a beginner, I won't try 
explaining that.

First, since I need a file with tabbed text, let Perl do it.
(It's not hard in C, so shouldn't be too hard in Perl.
 Didn't really need taint (-T) checks here, 
 but the warnings (-w) do help. 
 You'll need to remove the flags on the shebang line 
 and/or set the appropriate options in the script menu,
 to get these to compile.)
-------------------------------------------------------
#! perl -Tw

open ( TABBEDTEXT, ">tabbedText.text" ) 
  || die "Why couldn't I open tabbedText.text? $!";

# Does this look like a C programmer wrote it or what?

my( $i, $j );

for ( $i = 65; $i < 91; ++$i )
{
  for ( $j = 65; $j < 91; ++$j )
  {
    my $thing = pack( "CC", $i, $j );
    print TABBEDTEXT "$thing\t";
  }
  print TABBEDTEXT "\n";
}
-------------------------------------------------------
Here's your example:
(It looks long, but it's mostly explanation and  comments.)
-------------------------------------------------------
#! perl -Tw

open(INF,"tabbedText.text") 
 || die "Why couldn't I open tabbedText.text? $!";

my @datos = <INF>;     
# Grabs the whole file. For small files this is okay, so,
close(INF);
chomp( @datos );        #    ...then...:

# You can save typing with the implicit operands like $_
my $line;    	# I like to declare things. 
# Maybe it's a C habit.
my @dataArray;

foreach $line ( @datos )
{	# Oh. Now I see the other semantics problem here. 
  # You think you want a multi-dimensional array.
  # I think you probably would rather process 
  # a line at a time, but what do I know?
  # Multi-dimensional arrays are emulated 
  # via anonymous "reference"s.
  # It's an advanced subject, by the way.
  # And I need to practice it, so here goes:
  {
    my @splitLine = split(/\t/,$line);
    # Generate a reference to the split line array,
    # and save the reference.
    push( @dataArray, \@splitLine );
  }
  # @splitLine goes out of scope, but the saved reference 
  # keeps the contents from disappearing. Right?
  # And the next time through the loop, we get a new 
  # @splitLine. Right?
  # Hey, one of you guys who really know what you're 
  # doing here, did I need to nest that scope? 
  # Does it help prevent allocation tangles? 
  # Does it mean anything?
}

# Check the contents a more-or-less Perlish way:
my $line_a;
foreach $line_a ( @dataArray )
{
  # Should the vertical bar be escaped? Only on open()?
  print "|";  
  my $item;
  # The reference is a scalar, but it points to an array:
  foreach $item ( @$line_a ) 
  { print "$item|";
  }
  print "\n";
}
print "\n\n";

# Now we have the pseudo-multi-dimensioned array, let's 
# see if we can get the expected things out of it
# in a multi-dimensional kind of way. 
# (Although we probably wouldn't in production code?)

my $asize = @dataArray;
my $i;

for ( $i = 0; $i < $asize; ++$i )
{
  my $lsize = 26; # @$dataArray->[ $i ]; 
  my $j;
  
  for ( $j = 0; $j < $lsize; ++$j )
  {
    print "At $i and $j: $dataArray[ $i ]->[ $j ]\n"; 
    # And there is supposed to be a shortcut that looks 
    # even more like multi-dimensioned arrays.
  }
}





rees_joel@fujicomp.co.jp
http://www.fujicomp.co.jp
http://www.udit.gr.jp


==== Want to unsubscribe from this list?
==== Send mail with body "unsubscribe" to macperl-webcgi-request@macperl.org