http://qs321.pair.com?node_id=449160

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Assigning values. Simple and Nasty one.
by blazar (Canon) on Apr 19, 2005 at 09:54 UTC
    I know itz simple but no idea abt perl.
    IMHO, but others' mileage indeed may vary, this is SOPW, but not exactly a do-it-for-me helpdesk. So you'd better do a minimum effort to have some idea "abt perl" - all this is basic stuff.

    It would also be very kind of you to speak proper english rather than dude jargon.

    #!/usr/bin/perl #use warnings;
    Do a favour to yourself and uncomment that line. Do another favour to yourself and
    use strict;
    as well.
    open (GRAPH, "2211.txt") || die "Can't open 2211.txt: $!\n";4
    So far so fine, only I'd write
    open my $graph, '<', '2211.txt' or die "Can't open `2211.txt': $!\n";
    instead (slightly more modern syntax).
    while (<GRAPH>) { $a1 = push(@a1, $1); $a2 = push(@a2, $2); print @a1, @a2; }
    Huh?!? No regex here, hence pointless use of $1, $2. What are $a1 and $a2 supposed to be?!? In any case you most probably either want
    while (<GRAPH>) { /$some $regex ($with) ($parens)/; $a1 = $1; $a2 = $2; print $a1, $a2; }
    or
    my (@a1, @a2); while (<GRAPH>) { /$some $regex ($with) ($parens)/; push @a1, $1; push @a2, $2; } print @a1, @a2;
    It all really depends on
    • what "two columns in the txt" actually mean (whitepace separated strings?),
    • what you actually want to print.
Re: Assigning values. Simple and Nasty one.
by ambs (Pilgrim) on Apr 19, 2005 at 09:46 UTC
    Where are you getting '$1' and '$2'? They does not simply appear. Maybe you need to parse each file of the GRAPH file. But in that we can't help. You don't have explanation on the txt file syntax.

    Alberto Simões

Re: Assigning values. Simple and Nasty one.
by pearlie (Sexton) on Apr 19, 2005 at 10:44 UTC
    If you know the delimiter between the two columns and its fixed, you can use the "split" method in perl to extract the column values as follows:  @arr = split(/delimiter/,$_); Now the array "arr" contains all column values in 1 line of the file. Then you can print  $arr[0] and  $arr[1]. More can be elaborated on this if you specify the exact format of the file. Please go through perl documentation of split. That will make things far more clear.
      Thank you very much for your help. The delimeter is the whitespace and two columns contains numbers.
        So just go ahead and try using split. If it does not work, please post the code here.
        A reply falls below the community's threshold of quality. You may see it by logging in.