ODBiC Discussion Board


RE: WHILE Loop within an IMPORT loop?, David RIchardson, 11-12-2004

This should do what you were trying to do

by Roger Harris, November 14, 2004 19:49

I'm sorry, I didn't look closely enough at what you were doing, but using the "IMPORT * from file" in the new version may still not do what you need to do, since that doesn't really tell you what each of the columns is really named.

I think the following may be closer to what you want to do. On the first line read line from the file, it will use $split( ) to parse the line and use SETMULTI to put each of those column names in an instance of a variable called col_head. Then, for all lines after the first line, it will again read the full line, then use an EACHMULTI col_head loop to split the line, extracting one field from the line for each col_head and setting it in a variable called value. So, each pass through that EACHMULT loop, $col_head$ will be the name of a column and $value$ will be the data in the corresponding column:

<%
IMPORT line FROM $tempfile$.csv;
  IF $importrow$ = 1;
    WHILE $line$;
      SETMULTI col_head = $repl($split(line,","), '"'='');
    ENDWHILE;
  ELSE;
    EACHMULTI col_head;
      SET value = $repl($split(line,","), '"'='');
      %>
      $col_head$: $value$
      <%
    ENDMULTI;
  ENDIF;
ENDIMPORT;
%>

That's assuming that you can use just one column name and it's corresponding value on each pass through the EACHMULTI loop. Alternatively, you might extract the column names into variables name head_1 through head_n (keeping a count of how many columns there are), then extract the data in each line into variables numbered data_1 through data_n with something like this:

<%
IMPORT line FROM $tempfile$.csv;
  IF $importrow$ = 1;
    SET number_columns = 0;
    WHILE $line$;
      SET number_columns = $number_columns$ + 1,
          $var(head_$number_columns$) = $repl($split(line,","), '"'='');
    ENDWHILE;
  ELSE;
    SET col_number = 0;
    WHILE $line$;
      SET col_number = $col_number$ + 1,
          $var(data_$col_number$) = $repl($split(line,","), '"'='');
    ENDWHILE;
    %>
      $head_1$: $data_1$ <br>
      $head_2$: $data_2$ <br>
      $head_3$: $data_3$ <br>
      (etc.)
    <%
  ENDIF;
ENDIMPORT;
%>


Replies:


Post Your Reply:

Name   
E-mail  optional
Topic  
Message:

HTTP Link: 
Link text: 


Copyright 1997-2003, Roger Harris. All rights reserved.