Question about split

Discussion in 'Perl Misc' started by Hugh Kang, Nov 7, 2003.

  1. Hugh Kang

    Hugh Kang Guest

    I am trying to get the memory size of the system using prtconf (UnixWare 7)
    The output of prtconf is:

    SYSTEM CONFIGURATION:

    Memory Size: 2048 Megabytes
    System Peripherals:

    Floppy Drive 1 - 1.44 MB 3.5
    SCSI CD-Rom Drive 1 - TOSHIBA - DVD-ROM SD-M1502
    Tape Drive 1 - QUANTUM - DLT8000
    Disk Drive 1 - MYLEX - eXtremeRAID 2000 - 17406 MB
    Disk Drive 2 - MYLEX - eXtremeRAID 2000 - 52234 MB
    80387 Math Processor

    In the perl script, I am doing the followings:

    open(MEMSIZE,"/usr/sbin/prtconf") || die "Cannot run prtconf";
    while (<MEMSIZE>) {
    ($dummy, $word1, $word2, $memsize, $rest) = split;
    if ( $word1 eq "Memory" ) {
    print "dummy = $dummy \n";
    print "word1 = $word1 \n";
    print "word2 = $word2 \n";
    print "memsize = $memsize \n";
    print "rest = $rest \n";

    $memory = $memsize;
    print "memeory size = $memory \n";
    }
    }

    I am expecting '2048' for the memory size but I've got "%d" for $memsize.
    dummy =
    word1 = Memory
    word2 = Size:
    memsize = %d
    rest = Megabytes
    memeory size = %d

    Can anyone help me out with this? Any other good way to get the size?

    Regards
    Hugh
    Hugh Kang, Nov 7, 2003
    #1
    1. Advertising

  2. In article <>, Hugh Kang wrote:
    [cut]
    > open(MEMSIZE,"/usr/sbin/prtconf") || die "Cannot run prtconf";


    You're opening the executable file, you're not running it. Add
    a pipe ('|') after the file name.

    [cut]
    > I am expecting '2048' for the memory size but I've got "%d" for $memsize.


    Yes, you stumbled over the C format string in the compiled
    program. The '%d' is a placeholder for an integer value, just
    like in Perl's own printf function.

    --
    Andreas Kähäri
    Andreas Kahari, Nov 7, 2003
    #2
    1. Advertising

  3. Hugh Kang wrote:
    >
    > ($dummy, $word1, $word2, $memsize, $rest) = split;
    > if ( $word1 eq "Memory" ) {


    Since you are splitting without a pattern, it surprises me that
    $word1, and not $dummy, is assigned the first word on respective line.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Nov 7, 2003
    #3
  4. Hugh Kang

    Anno Siegel Guest

    Gunnar Hjalmarsson <> wrote in comp.lang.perl.misc:
    > Hugh Kang wrote:
    > >
    > > ($dummy, $word1, $word2, $memsize, $rest) = split;
    > > if ( $word1 eq "Memory" ) {

    >
    > Since you are splitting without a pattern, it surprises me that
    > $word1, and not $dummy, is assigned the first word on respective line.


    Well, he's reading the binary executablei. :) From the original code:

    open(MEMSIZE,"/usr/sbin/prtconf")

    By some coincidence, this puts "Memory" from the sprintf format into
    $word1.

    Anno
    Anno Siegel, Nov 10, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    457
  2. Carlos Ribeiro
    Replies:
    11
    Views:
    689
    Alex Martelli
    Sep 17, 2004
  3. trans.  (T. Onoma)

    split on '' (and another for split -1)

    trans. (T. Onoma), Dec 27, 2004, in forum: Ruby
    Replies:
    10
    Views:
    204
    Florian Gross
    Dec 28, 2004
  4. Sam Kong
    Replies:
    5
    Views:
    231
    Rick DeNatale
    Aug 12, 2006
  5. Stanley Xu
    Replies:
    2
    Views:
    595
    Stanley Xu
    Mar 23, 2011
Loading...

Share This Page