Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

extracting data from a file

by indy07 (Initiate)
on Dec 14, 2012 at 03:51 UTC ( #1008762=perlquestion: print w/ replies, xml ) Need Help??
indy07 has asked for the wisdom of the Perl Monks concerning the following question:

I have this file that contains the following info; "qazwsx" "edcrfv" "tgbyhn" I need to extract the characters between the " marks place them into seperate scalars. How would I do that in perl?

Comment on extracting data from a file
Re: extracting data from a file
by davido (Archbishop) on Dec 14, 2012 at 04:15 UTC

    I'm not comfortable that your description of the data-set is complete enough to provide a answer with any degree of confidence that it will "just work." But assuming you've got quoted fields delimited by a space character, use Text::CSV, and set sep_char => q{ }, # 0x20.

    CPAN is how you "do that in Perl."

    Update: An example is in order:

    use Text::CSV; my $csv = Text::CSV->new( { sep_char => q/ /, # 0x20 escape_char => q/\\/, # Single backslash escapes. } ); while( my $row = $csv->getline(\*DATA) ) { print '[', join( '] [', @{$row} ), "]\n"; } __DATA__ "qazwsx" "edcrfv" "tgbyhn" "asdf" "ghjk" "l;zx" "cv\"bn"

    ...which will produce the following output...

    [qazwsx] [edcrfv] [tgbyhn] [asdf] [ghjk] [l;zx] [cv"bn]

    I threw in an escaped quote character just for fun.


    Dave

Re: extracting data from a file
by frozenwithjoy (Curate) on Dec 14, 2012 at 07:17 UTC
    You could just capture stretches of non-quote characters that are between quotes:
    #!/usr/bin/env perl use strict; use warnings; use feature 'say'; my $string = q("qazwsx" "edcrfv" "tgbyhn"); my @array = $string =~ /"([^"]*)"/g; say $_ for @array; __END__ qazwsx edcrfv tgbyhn
Re: extracting data from a file
by nithins (Sexton) on Dec 14, 2012 at 07:21 UTC

    moving each word in to a variable that too dynamically creating it during the run time is not possible by the best of knowledge i know

    but u can dump it in to an hash which will create key value same as that of characters b/w " "

    hope this code might help u

    #!/usr/bin/perl use strict; use Data::Dumper; open ( FH ,'<' , 'D:\\myperlpgm\\nperl\\newfile.txt'); my %has; my @array; while(<FH>){ @array = split /"/; } print "@array\n"; for(@array){ $has{$_} = $_ ; } print Dumper ($has{ssss});

    D:\\myperlpgm\\nperl\\newfile.txt contains -> "nithn""nnnn""ssss" data

      Huh?

      What is the hash for?

      And take a closer look at what's in @array. By changing "print "@array\n"; to "print Dumper @array;", you will see that @array contains extra elements that result from splitting on the quote character as if it were a simple delimiter rather than a balanced construct.

      Also, given the OP's data set, there's no reason for the $hash{ssss} element to even exist.

      This is a broken solution.


      Dave

Re: extracting data from a file
by Anonymous Monk on Dec 14, 2012 at 07:29 UTC

    Start with Text::CSV esp with these programs that are in the Text::CSV_XS distribution
    examples/csv-check Script to check a CSV file/stream
    examples/csvdiff Script to shoff diff between sorted CSV files
    examples/parser-xs.pl Parse CSV stream, be forgiving on bad lines
    examples/speed.pl Small benchmark script

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1008762]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2014-09-16 18:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (42 votes), past polls