#!/usr/bin/perl
use strict;
open FH "filename.txt";
my %myhash = ();
while (my $line = <FH>) {
  if ($line =~ /(\S+)\s+(\S+)\s+\S+.*/) {
    $myhash{$1} = $2;
  }
}
...
__END__

-David

On Fri, 13 Aug 2004 12:05:16 -0500, Errin Larsen <[EMAIL PROTECTED]> wrote:
> ok.  I'm not getting my question across clearly.  Let me try again.
> 
> I have a collection of data in a text file.  Here is an example:
> 
> ## foobar.txt ##
> one  two  three
> A     B     C
> yes  no    maybe
> 
> In a script (not on the command line) I want to be able to parse this
> data so that the first two elements in each line of the file end up in
> a hash as a key/value pair.  If I was to declare this hash I'm
> refering to in a perl script in a literal context, I imagine it would
> look like this, given the above text file:
> 
> my %myhash = ( one=>two, A=>B, yes=>no );
> 
> Perhaps I'm wrong (I am rather new to this Perl stuff!).  However, I
> do not want to declare it literally, I want to parse the file to
> create that hash.  The code to parse the file, extract the first two
> elements of each line of that file and create a hash of key/value
> pairs using those first two elements is what I would like to know how
> to do!
> 
> Thx!
> 
> --Errin
> 
> 
> 
> 
> On 13 Aug 2004 16:57:00 +0100, Jose Alves de Castro <[EMAIL PROTECTED]> wrote:
> > On Fri, 2004-08-13 at 16:51, Errin Larsen wrote:
> > > um, can anyone explain the 'print' function below to me?
> > >
> > > specifically ... this:
> > >
> > >   'print "@F[0,5]"'
> >
> > The -a signal splits the input lines and stores the resulting elements
> > in @F
> >
> > Example:
> >
> > perl -nae 'print "$F[1]\n"' file.txt
> >
> > where file.txt contains
> >
> > one two three
> > four five six
> >
> > prints:
> >
> > two
> > five
> >
> > Also, although this splitting on spaces, you can also use the -F signal
> > to define what you're splitting in.
> >
> > See `perldoc perlrun`
> >
> > HTH,
> >
> > jac
> >
> > PS:
> >
> > Oh, print "@F[0,5]", of course, prints the first six elements of @F, and
> > since they're between double quotes, they're joined with whatever is in
> > $" (usually a space)
> >
> >
> >
> > > How do I use this idea in a script instead of a command line?  also,
> > > how is the input getting into this function?  I mean, I understand $_
> > > and all, but on a command line, are we piping to that command?  what's
> > > with the '@F'?
> > >
> > > Thanks for the help!
> > >
> > > --Errin
> > >
> > >
> > > On Fri, 13 Aug 2004 09:52:04 -0400, [EMAIL PROTECTED]
> > > <[EMAIL PROTECTED]> wrote:
> > > > Thanks too all who passed some knowledge on, but I ended up using :
> > > >
> > > >          while (<D>) {
> > > >
> > > >                 ## look for 9840S and ebexpire
> > > >                 ## declare OFS = tab
> > > >                 ## tell split to split on IRS 0,1&5. very similar to awk
> > > > print $
> > > >
> > > >                 if (($_ =~ /9840S/) && ($_ =~ /ebexpire, ebexpire/ )) {
> > > >                          local $, = "\t";
> > > >                         print FOO +(split)[0,1,5], $/;
> > > >                         #print +(split)[0,1,5], $/;
> > > >
> > > > Derek B. Smith
> > > > OhioHealth IT
> > > > UNIX / TSM / EDM Teams
> > > > 614-566-4145
> > > >
> > > > "John W. Krahn" <[EMAIL PROTECTED]>
> > > > 08/13/2004 08:51 AM
> > > >
> > > >         To:     Perl Beginners <[EMAIL PROTECTED]>
> > > >         cc:
> > > >         Subject:        Re: awk like question
> > > >
> > > > [EMAIL PROTECTED] wrote:
> > > > > All,
> > > >
> > > > Hello,
> > > >
> > > > > wasn't sure if this was received b/c I got a reurne to sender error..
> > > > >
> > > > >
> > > > > How can I print certain fields delimited by ' '?
> > > > > In awk I would write awk '{print $1, $6}' filename
> > > >
> > > > The Perl equivalent of that is:
> > > >
> > > > perl -lane 'print "@F[0,5]"'
> > > >
> > > > > Here is an out file that I want to grab data from :
> > > > >
> > > > > 04/29/04 11:00:28 [  6687:ebexpire, [EMAIL PROTECTED] E00796   9840S  537
> > > >
> > > > > 2B0234233543E6A4
> > > > > 04/29/04 11:00:28 [  6687:ebexpire, [EMAIL PROTECTED] E00830   9840S  571
> > > >
> > > > > D402325A8345ABDE
> > > > > 04/29/04 11:00:28 [  6687:ebexpire, [EMAIL PROTECTED] E00066   9840S  127
> > > >
> > > > > 5202333193B75CBB
> > > > > 04/29/04 11:00:28 [  6687:ebexpire, [EMAIL PROTECTED] E00501   9840S  168
> > > >
> > > > > 4B0233BABA5813F6
> > > > >
> > > > > I want fields one two and six or the date, time and E string.
> > > > > Does it matter whether I use a foreach or a while (<filehandle>)  ?
> > > >
> > > > You could write that in Perl as:
> > > >
> > > > perl -lane 'print "@F[0,1,5]"'
> > > >
> > > > John
> > > > --
> > > > use Perl;
> > > > program
> > > > fulfillment
> > > >
> > > > --
> > > > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > > > For additional commands, e-mail: [EMAIL PROTECTED]
> > > > <http://learn.perl.org/> <http://learn.perl.org/first-response>
> > > >
> > > >
> > --
> > José Alves de Castro <[EMAIL PROTECTED]>
> >   http://natura.di.uminho.pt/~jac
> >
> >
> >
> 
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
> 
>

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to