in reply to getting the right stuff

Text::ParseWords can help in splitting the string up into tokens, and it's really nice when getting rid of quotes that may or may not be there:
use Text::ParseWords; <DATA>; while (<DATA>) { chomp; my @words = shellwords($_); print "ID: $words[0]\tCourse: $words[1]\n"; } __END__ "ID" "Course" 9414 "Nutitional Epidemiology" 9414 "Nutritional assessment" 9414 "Undernutrition in the United States" 1371 "Health Politics and Policy" 1371 "Advanced Health Politics?" 1371 "Introduction to Health Policy & Management"
If you separate out the printing, it's probably best to return a reference to @records, then pass that to the printing subroutine. You can probably also pass a list of hash keys to print (use a hash slice or a subset):
my @keys = qw ( ID Dept Fax); print ul( map { li("$_: $data{$_}") } @keys);
I think that's what you mean.

Replies are listed 'Best First'.
Re: Re: getting the right stuff
by malaga (Pilgrim) on Feb 06, 2001 at 13:37 UTC
    i tried the Text::ParseWords, and it gave me back something like:
    : : : : : : : :
    i probably didn't do it right. but, going back to what i was doing before, once i have the data i need in @records, isn't there a way to get the " out with s///? or something simple?

    with the printing, what i want to do is lose the labels of ID, Dept, etc.

    i've been going over this - over and over - and i think i understand what i have right up to @records. i can draw a picture of where the data is going right up till then. but i'm not sure what the data looks like by the time it's in @records.

    i think what would probably solve all of my problems (because I would be able to call each item) is if i could assign a scalar variable to each item. so that i could say something like:
    print $ID, $firstname $middlename $lastname\n; print $dept, $address, $phone\n;
    does this make any sense?

      To get rid of a character from a string, use tr/"//d (you need to add the d on the end for tr to delete)... this is also really handy for a range of characters, e.g. tr/"'//d;. Of course you have to bind the tr operator to the string you want stripped as in $string =~ tr/A-Z/a-z/;

      HTH

      Philosophy can be made out of anything. Or less -- Jerry A. Fodor

        i don't know where to do the replacement. do i need to change @records into a scalar to do it?
      @records is just an ordered list. If you assigned to it directly, it would look something like:
      my @records; @records = qw( name ID email phone );
      That means the 0th element is 'name', and the 3rd element is 'phone'. In code terms:

      print "$records[0]\t$records[3]\n";

      produces name    phone. Does that help?

      If you find accessing data by name instead of by index is easier, you can assign to a hash:

      while (<DATA>) { chomp; my ($key, $value) = split; $value =~ tr/"//d; my %record; $record{$key} = $value; push @records, \%record; }
      You can then loop through records, printing just the elements you want:
      foreach my $rec (@records) { print "ID: $rec->{ID}\n"; print "Name: $rec->{name}\n"; }
      Is that more clear?
        the thing is i don't want to print ID, Name, etc. I want to print only the values. Gladys, Black, etc. i want to be able to control that. i want to be able to print it in a specific order.